<!DOCTYPE html>
<!-- saved from url=(0052)https://www.cnblogs.com/skychenjiajun/p/9037324.html -->
<html lang="zh-cn"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    <meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="referrer" content="origin">
    <title>springboot集成rabbitmq（实战） - 名山丶深处 - 博客园</title>
<meta property="og:description" content="RabbitMQ简介RabbitMQ使用Erlang语言开发的开源消息队列系统，基于AMQP协议来实现（AMQP的主要特征是面向消息、队列、路由、可靠性、安全）。支持多种客户端，如：Python、Ru">
    <link type="text/css" rel="stylesheet" href="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/blog-common.css">
<link id="MainCss" type="text/css" rel="stylesheet" href="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/bundle-CodingLife.css">
<link type="text/css" rel="stylesheet" href="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/404128.css">
<link id="mobile-style" media="only screen and (max-width: 767px)" type="text/css" rel="stylesheet" href="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/bundle-CodingLife-mobile.css">
    <link title="RSS" type="application/rss+xml" rel="alternate" href="https://www.cnblogs.com/skychenjiajun/rss">
    <link title="RSD" type="application/rsd+xml" rel="EditURI" href="https://www.cnblogs.com/skychenjiajun/rsd.xml">
<link type="application/wlwmanifest+xml" rel="wlwmanifest" href="https://www.cnblogs.com/skychenjiajun/wlwmanifest.xml">
    <script src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/f.txt"></script><script src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/pubads_impl_rendering_319.js.下载"></script><script async="" src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/analytics.js.下载"></script><script type="text/javascript" src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/encoder.js.下载"></script><script src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/jquery-2.2.0.min.js.下载"></script>
    <script>var currentBlogId=404128;var currentBlogApp='skychenjiajun',cb_enable_mathjax=false;var isLogined=false;</script>
    <script src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/blog-common.js.下载" type="text/javascript"></script>
<link rel="preload" href="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/f(1).txt" as="script"><script type="text/javascript" src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/f(1).txt"></script><script src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/pubads_impl_319.js.下载" async=""></script><link rel="prefetch" href="https://tpc.googlesyndication.com/safeframe/1-0-32/html/container.html"><style type="text/css">.heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}</style><script src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/share.js.下载"></script></head>
<body style="position: static;">
<a name="top"></a>

<div id="page_begin_html"><script type="text/javascript">window['__document_write_ajax_callbacks__']['2']();</script><script type="text/javascript">window['__document_write_ajax_callbacks__']['1']();</script></div><script>load_page_begin_html();</script>

<!--done-->
<div id="home">
<div id="header">
	<div id="blogTitle">
	<a id="lnkBlogLogo" href="https://www.cnblogs.com/skychenjiajun/"><img id="blogLogo" src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/logo.gif" alt="返回主页"></a>			
		
<!--done-->
<h1><a id="Header1_HeaderTitle" class="headermaintitle" href="https://www.cnblogs.com/skychenjiajun/">名山丶深处</a></h1>
<h2></h2>



		
	</div><!--end: blogTitle 博客的标题和副标题 -->
	<div id="navigator">
		
<ul id="navList">
<li><a id="blog_nav_sitehome" class="menu" href="https://www.cnblogs.com/">博客园</a></li>
<li><a id="blog_nav_myhome" class="menu" href="https://www.cnblogs.com/skychenjiajun/">首页</a></li>
<li><a id="blog_nav_newpost" class="menu" rel="nofollow" href="https://i.cnblogs.com/EditPosts.aspx?opt=1">新随笔</a></li>
<li><a id="blog_nav_contact" class="menu" rel="nofollow" href="https://msg.cnblogs.com/send/%E5%90%8D%E5%B1%B1%E4%B8%B6%E6%B7%B1%E5%A4%84">联系</a></li>
<li><a id="blog_nav_rss" class="menu" href="https://www.cnblogs.com/skychenjiajun/rss">订阅</a>
<!--<a id="blog_nav_rss_image" class="aHeaderXML" href="https://www.cnblogs.com/skychenjiajun/rss"><img src="//www.cnblogs.com/images/xml.gif" alt="订阅" /></a>--></li>
<li><a id="blog_nav_admin" class="menu" rel="nofollow" href="https://i.cnblogs.com/">管理</a></li>
</ul>
		<div class="blogStats">
			
			<div id="blog_stats">
<span id="stats_post_count">随笔 - 17&nbsp; </span>
<span id="stats_article_count">文章 - 0&nbsp; </span>
<span id="stats-comment_count">评论 - 12</span>
</div>
			
		</div><!--end: blogStats -->
	</div><!--end: navigator 博客导航栏 -->
</div><!--end: header 头部 -->

<div id="main">
	<div id="mainContent">
	<div class="forFlow">
		
        <div id="post_detail">
<!--done-->
<div id="topics">
	<div class="post">
		<h1 class="postTitle">
			<a id="cb_post_title_url" class="postTitle2" href="https://www.cnblogs.com/skychenjiajun/p/9037324.html">springboot集成rabbitmq（实战）</a>
		</h1>
		<div class="clear"></div>
		<div class="postBody">
			<div id="cnblogs_post_body" class="blogpost-body"><p><span style="font-size: 18pt;">RabbitMQ简介</span><br><span style="font-family: 宋体;">RabbitMQ使用Erlang语言开发的开源消息队列系统，基于AMQP协议来实现（AMQP的主要特征是面向消息、队列、路由、可靠性、安全）。支持多种客户端，如：Python、Ruby、.NET、Java、JMS、C、PHP、ActionScript、XMPP、STOMP等，支持AJAX。用于在分布式系统中存储转发消息，在易用性、扩展性、高可用性等方面表现很出色。</span></p>
<p>&nbsp;</p>
<p><span style="font-family: 宋体;"><strong><span style="font-size: 14px;">相关概念</span></strong></span></p>
<p><span style="font-family: 宋体;">消息队列通常有三个概念：发送消息（生产者）、队列、接收消息（消费者）。RabbitMQ在这个基本概念之上，多做了一层抽象，在发送消息和队列之间，加入了交换机。这样发送消息和队列就没有直接关系，而是通过交换机来做转发，交换机会根据分发策略把消息转给队列。</span></p>
<p><span style="font-family: 宋体;">图一（MQ基本模型）：</span></p>
<p><span style="font-family: 宋体;">P为发送消息（生产者）、Q为消息队列、C为接收消息（消费者）</span></p>
<p>&nbsp;</p>
<p><span style="font-family: 宋体;">图二（RabbitMQ模型）：</span></p>
<p><span style="font-family: 宋体;">P为发送消息（生产者）、<span style="color: #000080;">X为交换机</span>、Q为消息队列、C为接收消息（消费者）</span></p>
<p><span style="font-family: 宋体;"><img src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/1308658-20180521182608882-979039140.png" alt="">　　&nbsp;<img src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/1308658-20180521182545059-1155725091.png" alt=""></span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 图一 　　　　　　　　　　　　　　　　　　　　　　　　　　　　图二</p>
<p><span style="font-family: 宋体;"><strong>RabbitMQ比较重要的几个概念：</strong></span></p>
<p><span style="font-family: 宋体;">虚拟主机：RabbitMQ支持权限控制，但是最小控制粒度为虚拟主机。一个虚拟主机可以包含多个交换机、队列、绑定。</span></p>
<p><span style="font-family: 宋体;">交换机：RabbitMQ分发器，根据不同的策略将消息分发到相关的队列。</span></p>
<p><span style="font-family: 宋体;">队列：缓存消息的容器。</span></p>
<p><span style="font-family: 宋体;">绑定：设置交换机与队列的关系。</span></p>
<p>&nbsp;</p>
<p><span style="font-family: 宋体; font-size: 14px;"><strong>是时候表演真正的技术了</strong></span></p>
<p><span style="font-size: 14px; font-family: 宋体;">为了方便演示，我们分别创建两个springboot项目：</span></p>
<p><span style="font-size: 14px; font-family: 宋体;">spring-boot-rabbitmq-producer（生产者）</span></p>
<p><span style="font-size: 14px; font-family: 宋体;">spring-boot-rabbitmq-consumer（消费者）</span></p>
<p><span style="font-size: 14px; font-family: 宋体;">注意：实际项目中，一个系统可能即为生产者、又为消费者。</span></p>
<p>&nbsp;</p>
<p><span style="font-size: 14px;"><strong>1.添加基础配置</strong></span></p>
<p><span style="font-size: 14px;">生产者、消费者基础配置相同。</span></p>
<p>1.1）集成rabbitmq，添加maven依赖</p>
<div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="复制代码"><img src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/copycode.gif" alt="复制代码"></a></span></div>
<pre><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dependencies</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">&gt;</span>org.springframework.boot<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">&gt;</span>spring-boot-starter<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">&gt;</span>
    
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">&gt;</span>org.springframework.boot<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">&gt;</span>spring-boot-starter-amqp<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">&gt;</span>

    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">&gt;</span>org.springframework.boot<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">&gt;</span>spring-boot-starter-test<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">scope</span><span style="color: #0000ff;">&gt;</span>test<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">scope</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">dependencies</span><span style="color: #0000ff;">&gt;</span></pre>
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="复制代码"><img src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/copycode.gif" alt="复制代码"></a></span></div></div>
<p>&nbsp;</p>
<p>&nbsp;1.2）添加rabbitmq服务配置（application.properties）</p>
<div class="cnblogs_code">
<pre><span style="color: #000000;">#rabbitmq相关配置
spring.rabbitmq.host=192.168.15.131
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=123456</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>2.交换机——DirectExchange</strong></p>
<p>DirectExchange是RabbitMQ的默认交换机，直接使用routingKey匹配队列。</p>
<p><img src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/1308658-20180515095018484-830578829.png" alt=""></p>
<p>2.1）添加一个配置类（消费者）</p>
<p>配置一个routingKey为notify.payment的消息队列</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_460456" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Configuration</code></div><div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">DirectConfig {</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Bean</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Queue paymentNotifyQueue() {</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java keyword">new</code> <code class="java plain">Queue(</code><code class="java string">"notify.payment"</code><code class="java plain">);</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number7 index6 alt2"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>&nbsp;</p>
<p>2.2）添加一个消息监听类（消费者）</p>
<p>监听routingKey为notify.payment的队列消息</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_144372" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Component</code></div><div class="line number2 index1 alt1"><code class="java color1">@RabbitListener</code><code class="java plain">(queues = </code><code class="java string">"notify.payment"</code><code class="java plain">)</code></div><div class="line number3 index2 alt2"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">PaymentNotifyReceive {</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@RabbitHandler</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">receive(String msg) {</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"notify.payment receive message: "</code><code class="java plain">+msg);</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number8 index7 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>2.3）添加一个消息发送类（生产者）</p>
<p>将消息发送至默认的交换机且routingKey为notify.payment</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_983461" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Component</code></div><div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">PaymentNotifySender {</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Autowired</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java plain">AmqpTemplate rabbitTemplate;</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">sender(String msg){</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"notify.payment send message: "</code><code class="java plain">+msg);</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">rabbitTemplate.convertAndSend(</code><code class="java string">"notify.payment"</code><code class="java plain">, msg);</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number10 index9 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>2.4）添加一个测试类（生产者）</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_285319" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@RunWith</code><code class="java plain">(SpringRunner.</code><code class="java keyword">class</code><code class="java plain">)</code></div><div class="line number2 index1 alt1"><code class="java color1">@SpringBootTest</code></div><div class="line number3 index2 alt2"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">PaymentNotifySenderTests {</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Autowired</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java plain">PaymentNotifySender sender;</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Test</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">test_sender() {</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sender.sender(</code><code class="java string">"支付订单号："</code><code class="java plain">+System.currentTimeMillis());</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number11 index10 alt2"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>&nbsp;</p>
<p>2.5）执行test_sender()方法</p>
<p>生产者日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_782290" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:28:53.264&nbsp; INFO 10624 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.PaymentNotifySenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Starting PaymentNotifySenderTests on LAPTOP-1DF7S904 with PID 10624 (started by lianjinsoft...</code></div><div class="line number2 index1 alt1"><code class="html plain">2018-05-14 16:28:53.265&nbsp; INFO 10624 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.PaymentNotifySenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : No active profile set, falling back to default profiles: default</code></div><div class="line number3 index2 alt2"><code class="html plain">2018-05-14 16:28:53.305&nbsp; INFO 10624 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@...</code></div><div class="line number4 index3 alt1"><code class="html plain">2018-05-14 16:28:54.133&nbsp; INFO 10624 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type ...</code></div><div class="line number5 index4 alt2"><code class="html plain">2018-05-14 16:28:55.104&nbsp; INFO 10624 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Starting beans in phase 2147483647</code></div><div class="line number6 index5 alt1"><code class="html plain">2018-05-14 16:28:55.114&nbsp; INFO 10624 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.PaymentNotifySenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Started PaymentNotifySenderTests in 2.246 seconds (JVM running for 3.199)</code></div><div class="line number7 index6 alt2"><code class="html plain">2018-05-14 16:28:55.343&nbsp; INFO 10624 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：1526286535342</code></div><div class="line number8 index7 alt1"><code class="html plain">2018-05-14 16:28:55.383&nbsp; INFO 10624 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Attempting to connect to: [192.168.15.131:5672]</code></div><div class="line number9 index8 alt2"><code class="html plain">2018-05-14 16:28:55.444&nbsp; INFO 10624 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Created new connection: rabbitConnectionFactory#38be305c:0/SimpleConnection@71984c3 ...</code></div><div class="line number10 index9 alt1"><code class="html plain">2018-05-14 16:28:55.483&nbsp; INFO 10624 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7eac9008: ...</code></div><div class="line number11 index10 alt2"><code class="html plain">2018-05-14 16:28:55.485&nbsp; INFO 10624 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Stopping beans in phase 2147483647</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>消费者日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_397877" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:28:55.490&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：1526286535342</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>分析日志：</p>
<p>从生产者日志第7行可以看出，消息已经成功发送。</p>
<p>从消费者日志可以看出，消息已经成功接收。</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>3.交换机——TopicExchange</strong></p>
<p>TopicExchange是按规则转发消息，是交换机中最灵活的一个。也是最常用的一个。</p>
<p><img src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/1308658-20180515095821882-1047782467.png" alt=""></p>
<p>3.1）添加一个配置类（消费者）</p>
<p>配置一个routingKey为api.core的消息队列并绑定在coreExchange交换机上（交换机的匹配规则为api.core.*）</p>
<p>配置一个routingKey为api.payment的消息队列并绑定在paymentExchange交换机上（交换机的匹配规则为api.payment.#）</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_1303" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div><div class="line number28 index27 alt1">28</div><div class="line number29 index28 alt2">29</div><div class="line number30 index29 alt1">30</div><div class="line number31 index30 alt2">31</div><div class="line number32 index31 alt1">32</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Configuration</code></div><div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">TopicConfig {</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Bean</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Queue coreQueue() {</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java keyword">new</code> <code class="java plain">Queue(</code><code class="java string">"api.core"</code><code class="java plain">);</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Bean</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Queue paymentQueue() {</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java keyword">new</code> <code class="java plain">Queue(</code><code class="java string">"api.payment"</code><code class="java plain">);</code></div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number13 index12 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Bean</code></div><div class="line number14 index13 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">TopicExchange coreExchange() {</code></div><div class="line number15 index14 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java keyword">new</code> <code class="java plain">TopicExchange(</code><code class="java string">"coreExchange"</code><code class="java plain">);</code></div><div class="line number16 index15 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number17 index16 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number18 index17 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Bean</code></div><div class="line number19 index18 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">TopicExchange paymentExchange() {</code></div><div class="line number20 index19 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java keyword">new</code> <code class="java plain">TopicExchange(</code><code class="java string">"paymentExchange"</code><code class="java plain">);</code></div><div class="line number21 index20 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number22 index21 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number23 index22 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Bean</code></div><div class="line number24 index23 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Binding bindingCoreExchange(Queue coreQueue, TopicExchange coreExchange) {</code></div><div class="line number25 index24 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java plain">BindingBuilder.bind(coreQueue).to(coreExchange).with(</code><code class="java string">"api.core.*"</code><code class="java plain">);</code></div><div class="line number26 index25 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number27 index26 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number28 index27 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Bean</code></div><div class="line number29 index28 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Binding bindingPaymentExchange(Queue paymentQueue, TopicExchange paymentExchange) {</code></div><div class="line number30 index29 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java plain">BindingBuilder.bind(paymentQueue).to(paymentExchange).with(</code><code class="java string">"api.payment.#"</code><code class="java plain">);</code></div><div class="line number31 index30 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number32 index31 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>3.2）添加两个消息监听类（消费者）</p>
<p>监听routingKey为api.core的队列消息</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_704135" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Component</code></div><div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">ApiCoreReceive {</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@RabbitHandler</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@RabbitListener</code><code class="java plain">(queues = </code><code class="java string">"api.core"</code><code class="java plain">)</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">user(String msg) {</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"api.core receive message: "</code><code class="java plain">+msg);</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number8 index7 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>监听routingKey为api.payment的队列消息</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_568806" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Component</code></div><div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">ApiPaymentReceive {</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@RabbitHandler</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@RabbitListener</code><code class="java plain">(queues = </code><code class="java string">"api.payment"</code><code class="java plain">)</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">order(String msg) {</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"api.payment.order receive message: "</code><code class="java plain">+msg);</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number8 index7 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>3.3）添加两个消息发送类（生产者）</p>
<p>添加一个user()方法，发送消息至coreExchange交换机且routingKey为api.core.user</p>
<p>添加一个userQuery()方法，发送消息至coreExchange交换机且routingKey为api.core.user.query</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_290442" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Component</code></div><div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">ApiCoreSender {</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Autowired</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java plain">AmqpTemplate rabbitTemplate;</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">user(String msg){</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"api.core.user send message: "</code><code class="java plain">+msg);</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">rabbitTemplate.convertAndSend(</code><code class="java string">"coreExchange"</code><code class="java plain">, </code><code class="java string">"api.core.user"</code><code class="java plain">, msg);</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">userQuery(String msg){</code></div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"api.core.user.query send message: "</code><code class="java plain">+msg);</code></div><div class="line number13 index12 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">rabbitTemplate.convertAndSend(</code><code class="java string">"coreExchange"</code><code class="java plain">, </code><code class="java string">"api.core.user.query"</code><code class="java plain">, msg);</code></div><div class="line number14 index13 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number15 index14 alt2"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>添加一个order()方法，发送消息至paymentExchange交换机且routingKey为api.payment.order</p>
<p>添加一个orderQuery()方法，发送消息至paymentExchange交换机且routingKey为api.payment.order.query</p>
<p>添加一个orderDetailQuery()方法，发送消息至paymentExchange交换机且routingKey为api.payment.order.detail.query</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_334371" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Component</code></div><div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">ApiPaymentSender {</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Autowired</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java plain">AmqpTemplate rabbitTemplate;</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">order(String msg){</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"api.payment.order send message: "</code><code class="java plain">+msg);</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">rabbitTemplate.convertAndSend(</code><code class="java string">"paymentExchange"</code><code class="java plain">, </code><code class="java string">"api.payment.order"</code><code class="java plain">, msg);</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">orderQuery(String msg){</code></div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"api.payment.order.query send message: "</code><code class="java plain">+msg);</code></div><div class="line number13 index12 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">rabbitTemplate.convertAndSend(</code><code class="java string">"paymentExchange"</code><code class="java plain">, </code><code class="java string">"api.payment.order.query"</code><code class="java plain">, msg);</code></div><div class="line number14 index13 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number15 index14 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number16 index15 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">orderDetailQuery(String msg){</code></div><div class="line number17 index16 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"api.payment.order.detail.query send message: "</code><code class="java plain">+msg);</code></div><div class="line number18 index17 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">rabbitTemplate.convertAndSend(</code><code class="java string">"paymentExchange"</code><code class="java plain">, </code><code class="java string">"api.payment.order.detail.query"</code><code class="java plain">, msg);</code></div><div class="line number19 index18 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number20 index19 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>3.4）添加两个测试类（生产者）</p>
<p>测试ApiCoreSender类中的相关方法</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_370606" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@RunWith</code><code class="java plain">(SpringRunner.</code><code class="java keyword">class</code><code class="java plain">)</code></div><div class="line number2 index1 alt1"><code class="java color1">@SpringBootTest</code></div><div class="line number3 index2 alt2"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">ApiCoreSenderTests {</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Autowired</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java plain">ApiCoreSender sender;</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Test</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">test_user() {</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sender.user(</code><code class="java string">"用户管理！"</code><code class="java plain">);</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Test</code></div><div class="line number13 index12 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">test_userQuery() {</code></div><div class="line number14 index13 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sender.userQuery(</code><code class="java string">"查询用户信息！"</code><code class="java plain">);</code></div><div class="line number15 index14 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number16 index15 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>测试ApiPaymentSender类中的相关方法</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_636463" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@RunWith</code><code class="java plain">(SpringRunner.</code><code class="java keyword">class</code><code class="java plain">)</code></div><div class="line number2 index1 alt1"><code class="java color1">@SpringBootTest</code></div><div class="line number3 index2 alt2"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">ApiPaymentSenderTests {</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Autowired</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java plain">ApiPaymentSender sender;</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Test</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">test_order() {</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sender.order(</code><code class="java string">"订单管理！"</code><code class="java plain">);</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Test</code></div><div class="line number13 index12 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">test_orderQuery() {</code></div><div class="line number14 index13 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sender.orderQuery(</code><code class="java string">"查询订单信息！"</code><code class="java plain">);</code></div><div class="line number15 index14 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number16 index15 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number17 index16 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Test</code></div><div class="line number18 index17 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">test_orderDetailQuery() {</code></div><div class="line number19 index18 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sender.orderDetailQuery(</code><code class="java string">"查询订单详情信息！"</code><code class="java plain">);</code></div><div class="line number20 index19 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number21 index20 alt2"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　</p>
<p>3.5）验证</p>
<p>3.5.1）执行ApiCoreSenderTests测试类</p>
<p>生产者日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_819104" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:30:05.804&nbsp; INFO 7340 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.lianjinsoft.sender.ApiCoreSenderTests&nbsp; : Starting ApiCoreSenderTests on LAPTOP-1DF7S904 with PID 7340 (started by lianjinsoft in ...</code></div><div class="line number2 index1 alt1"><code class="html plain">2018-05-14 16:30:05.805&nbsp; INFO 7340 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.lianjinsoft.sender.ApiCoreSenderTests&nbsp; : No active profile set, falling back to default profiles: default</code></div><div class="line number3 index2 alt2"><code class="html plain">2018-05-14 16:30:05.851&nbsp; INFO 7340 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1e800aaa: ...</code></div><div class="line number4 index3 alt1"><code class="html plain">2018-05-14 16:30:06.553&nbsp; INFO 7340 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type ...</code></div><div class="line number5 index4 alt2"><code class="html plain">2018-05-14 16:30:07.375&nbsp; INFO 7340 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Starting beans in phase 2147483647</code></div><div class="line number6 index5 alt1"><code class="html plain">2018-05-14 16:30:07.385&nbsp; INFO 7340 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.lianjinsoft.sender.ApiCoreSenderTests&nbsp; : Started ApiCoreSenderTests in 1.922 seconds (JVM running for 2.846)</code></div><div class="line number7 index6 alt2"><code class="html plain">2018-05-14 16:30:07.431&nbsp; INFO 7340 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : api.core.user send message: 用户管理！</code></div><div class="line number8 index7 alt1"><code class="html plain">2018-05-14 16:30:07.463&nbsp; INFO 7340 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Attempting to connect to: [192.168.15.131:5672]</code></div><div class="line number9 index8 alt2"><code class="html plain">2018-05-14 16:30:07.578&nbsp; INFO 7340 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Created new connection: rabbitConnectionFactory#56ace400:0/SimpleConnection@773cbf4f...</code></div><div class="line number10 index9 alt1"><code class="html plain">2018-05-14 16:30:07.647&nbsp; INFO 7340 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : api.core.user.query send message: 查询用户信息！</code></div><div class="line number11 index10 alt2"><code class="html plain">2018-05-14 16:30:07.716&nbsp; INFO 7340 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1e800aaa: ...</code></div><div class="line number12 index11 alt1"><code class="html plain">2018-05-14 16:30:07.728&nbsp; INFO 7340 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Stopping beans in phase 2147483647</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>消费者日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_648294" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:30:07.609&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : api.core receive message: 用户管理！</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>分析日志：</p>
<p>从生产者日志第7、10行可以看出，api.core.user和api.core.user.query消息均已发送成功。</p>
<p>从消费者日志可以看出，只有api.core.user发送的消息被收到了。</p>
<p>问题：</p>
<p>为什么api.core.user.query发送的消息没有被api.core队列监听消费？</p>
<p>答：因为在TopicConfig配置类中，我们对api.core队列绑定的交换机规则是api.core.*，而通配符“*”只能向后多匹配一层路径。</p>
<p>&nbsp;</p>
<p>3.5.2）执行ApiPaymentSenderTests测试类</p>
<p>生产者日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_192369" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:31:12.823&nbsp; INFO 6460 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.ApiPaymentSenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Starting ApiPaymentSenderTests on LAPTOP-1DF7S904 with PID 6460 (started by lianjinsoft in ...</code></div><div class="line number2 index1 alt1"><code class="html plain">2018-05-14 16:31:12.823&nbsp; INFO 6460 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.ApiPaymentSenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : No active profile set, falling back to default profiles: default</code></div><div class="line number3 index2 alt2"><code class="html plain">2018-05-14 16:31:12.857&nbsp; INFO 6460 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1e800aaa: ...</code></div><div class="line number4 index3 alt1"><code class="html plain">2018-05-14 16:31:13.718&nbsp; INFO 6460 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type ...</code></div><div class="line number5 index4 alt2"><code class="html plain">2018-05-14 16:31:14.530&nbsp; INFO 6460 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Starting beans in phase 2147483647</code></div><div class="line number6 index5 alt1"><code class="html plain">2018-05-14 16:31:14.539&nbsp; INFO 6460 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.ApiPaymentSenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Started ApiPaymentSenderTests in 2.05 seconds (JVM running for 2.945)</code></div><div class="line number7 index6 alt2"><code class="html plain">2018-05-14 16:31:14.592&nbsp; INFO 6460 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : api.payment.order.query send message: 查询订单信息！</code></div><div class="line number8 index7 alt1"><code class="html plain">2018-05-14 16:31:14.638&nbsp; INFO 6460 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Attempting to connect to: [192.168.15.131:5672]</code></div><div class="line number9 index8 alt2"><code class="html plain">2018-05-14 16:31:14.762&nbsp; INFO 6460 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Created new connection: rabbitConnectionFactory#47404bea:0/SimpleConnection@6b54655f ...</code></div><div class="line number10 index9 alt1"><code class="html plain">2018-05-14 16:31:14.819&nbsp; INFO 6460 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : api.payment.order.detail.query send message: 查询订单详情信息！</code></div><div class="line number11 index10 alt2"><code class="html plain">2018-05-14 16:31:14.825&nbsp; INFO 6460 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : api.payment.order send message: 订单管理！</code></div><div class="line number12 index11 alt1"><code class="html plain">2018-05-14 16:31:14.836&nbsp; INFO 6460 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1e800aaa: ...</code></div><div class="line number13 index12 alt2"><code class="html plain">2018-05-14 16:31:14.840&nbsp; INFO 6460 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Stopping beans in phase 2147483647</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>消费者日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_117803" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:31:14.809&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : api.payment.order receive message: 查询订单信息！</code></div><div class="line number2 index1 alt1"><code class="html plain">2018-05-14 16:31:14.821&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : api.payment.order receive message: 查询订单详情信息！</code></div><div class="line number3 index2 alt2"><code class="html plain">2018-05-14 16:31:14.829&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : api.payment.order receive message: 订单管理！</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>分析日志：</p>
<p>从生产者日志第7、10、11行可以看出，api.payment.order.query、api.payment.order.detail.query、api.payment.order消息均发送成功。</p>
<p>从消费者日志可以看出，api.payment.order队列监听到了所有消息并均处理成功了。</p>
<p>&nbsp;</p>
<p><strong>知识点：</strong></p>
<p><span style="color: #000000;">TopicExchange交换机支持使用通配符*、#</span></p>
<p><span style="color: #000000;">*号只能向后多匹配一层路径。</span></p>
<p><span style="color: #000000;">#号可以向后匹配多层路径。</span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>4.交换机——HeadersExchange</strong></p>
<p>HeadersExchange交换机是根据请求消息中设置的header attribute参数类型来匹配的（和routingKey没有关系）。</p>
<p>4.1）添加一个配置类（消费者）</p>
<p>配置一个routingKey为credit.bank的消息队列并绑定在creditBankExchange交换机上</p>
<p>配置一个routingKey为credit.finance的消息队列并绑定在creditFinanceExchange交换机上</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_762085" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div><div class="line number28 index27 alt1">28</div><div class="line number29 index28 alt2">29</div><div class="line number30 index29 alt1">30</div><div class="line number31 index30 alt2">31</div><div class="line number32 index31 alt1">32</div><div class="line number33 index32 alt2">33</div><div class="line number34 index33 alt1">34</div><div class="line number35 index34 alt2">35</div><div class="line number36 index35 alt1">36</div><div class="line number37 index36 alt2">37</div><div class="line number38 index37 alt1">38</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Configuration</code></div><div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">HeadersConfig {</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Bean</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Queue creditBankQueue() {</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java keyword">new</code> <code class="java plain">Queue(</code><code class="java string">"credit.bank"</code><code class="java plain">);</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Bean</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Queue creditFinanceQueue() {</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java keyword">new</code> <code class="java plain">Queue(</code><code class="java string">"credit.finance"</code><code class="java plain">);</code></div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number13 index12 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Bean</code></div><div class="line number14 index13 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">HeadersExchange creditBankExchange() {</code></div><div class="line number15 index14 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java keyword">new</code> <code class="java plain">HeadersExchange(</code><code class="java string">"creditBankExchange"</code><code class="java plain">);</code></div><div class="line number16 index15 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number17 index16 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number18 index17 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Bean</code></div><div class="line number19 index18 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">HeadersExchange creditFinanceExchange() {</code></div><div class="line number20 index19 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java keyword">new</code> <code class="java plain">HeadersExchange(</code><code class="java string">"creditFinanceExchange"</code><code class="java plain">);</code></div><div class="line number21 index20 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number22 index21 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number23 index22 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Bean</code></div><div class="line number24 index23 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Binding bindingCreditAExchange(Queue creditBankQueue, HeadersExchange creditBankExchange) {</code></div><div class="line number25 index24 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">Map&lt;String,Object&gt; headerValues = </code><code class="java keyword">new</code> <code class="java plain">HashMap&lt;&gt;();</code></div><div class="line number26 index25 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">headerValues.put(</code><code class="java string">"type"</code><code class="java plain">, </code><code class="java string">"cash"</code><code class="java plain">);</code></div><div class="line number27 index26 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">headerValues.put(</code><code class="java string">"aging"</code><code class="java plain">, </code><code class="java string">"fast"</code><code class="java plain">);</code></div><div class="line number28 index27 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java plain">BindingBuilder.bind(creditBankQueue).to(creditBankExchange).whereAll(headerValues).match();</code></div><div class="line number29 index28 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number30 index29 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number31 index30 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Bean</code></div><div class="line number32 index31 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Binding bindingCreditBExchange(Queue creditFinanceQueue, HeadersExchange creditFinanceExchange) {</code></div><div class="line number33 index32 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">Map&lt;String,Object&gt; headerValues = </code><code class="java keyword">new</code> <code class="java plain">HashMap&lt;&gt;();</code></div><div class="line number34 index33 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">headerValues.put(</code><code class="java string">"type"</code><code class="java plain">, </code><code class="java string">"cash"</code><code class="java plain">);</code></div><div class="line number35 index34 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">headerValues.put(</code><code class="java string">"aging"</code><code class="java plain">, </code><code class="java string">"fast"</code><code class="java plain">);</code></div><div class="line number36 index35 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java plain">BindingBuilder.bind(creditFinanceQueue).to(creditFinanceExchange).whereAny(headerValues).match();</code></div><div class="line number37 index36 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number38 index37 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>4.2）添加一个消息监听类（消费者）</p>
<p>添加creditBank()方法，监听routingKey为credit.bank的队列消息</p>
<p>添加creditFinance()方法，监听routingKey为credit.finance的队列消息</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_522786" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Component</code></div><div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">ApiCreditReceive {</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@RabbitHandler</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@RabbitListener</code><code class="java plain">(queues = </code><code class="java string">"credit.bank"</code><code class="java plain">)</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">creditBank(String msg) {</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"credit.bank receive message: "</code><code class="java plain">+msg);</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@RabbitHandler</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@RabbitListener</code><code class="java plain">(queues = </code><code class="java string">"credit.finance"</code><code class="java plain">)</code></div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">creditFinance(String msg) {</code></div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"credit.finance receive message: "</code><code class="java plain">+msg);</code></div><div class="line number13 index12 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number14 index13 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>4.3）添加一个消息发送类（生产者）</p>
<p>添加一个creditBank()方法，发送消息至creditBankExchange交换机且routingKey为credit.bank</p>
<p>添加一个creditFinance()方法，发送消息至creditFinanceExchange交换机且routingKey为credit.finance</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_788995" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Component</code></div><div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">ApiCreditSender {</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Autowired</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java plain">AmqpTemplate rabbitTemplate;</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">creditBank(Map&lt;String, Object&gt; head, String msg){</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"credit.bank send message: "</code><code class="java plain">+msg);</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">rabbitTemplate.convertAndSend(</code><code class="java string">"creditBankExchange"</code><code class="java plain">, </code><code class="java string">"credit.bank"</code><code class="java plain">, getMessage(head, msg));</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">creditFinance(Map&lt;String, Object&gt; head, String msg){</code></div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"credit.finance send message: "</code><code class="java plain">+msg);</code></div><div class="line number13 index12 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">rabbitTemplate.convertAndSend(</code><code class="java string">"creditFinanceExchange"</code><code class="java plain">, </code><code class="java string">"credit.finance"</code><code class="java plain">, getMessage(head, msg));</code></div><div class="line number14 index13 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number15 index14 alt2"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>4.4）添加一个测试类（生产者）</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_621219" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div><div class="line number28 index27 alt1">28</div><div class="line number29 index28 alt2">29</div><div class="line number30 index29 alt1">30</div><div class="line number31 index30 alt2">31</div><div class="line number32 index31 alt1">32</div><div class="line number33 index32 alt2">33</div><div class="line number34 index33 alt1">34</div><div class="line number35 index34 alt2">35</div><div class="line number36 index35 alt1">36</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@RunWith</code><code class="java plain">(SpringRunner.</code><code class="java keyword">class</code><code class="java plain">)</code></div><div class="line number2 index1 alt1"><code class="java color1">@SpringBootTest</code></div><div class="line number3 index2 alt2"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">ApiCreditSenderTests {</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Autowired</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java plain">ApiCreditSender sender;</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Test</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">test_creditBank_type() {</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">Map&lt;String,Object&gt; head = </code><code class="java keyword">new</code> <code class="java plain">HashMap&lt;&gt;();</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">head.put(</code><code class="java string">"type"</code><code class="java plain">, </code><code class="java string">"cash"</code><code class="java plain">);</code></div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sender.creditBank(head, </code><code class="java string">"银行授信(部分匹配)"</code><code class="java plain">);</code></div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number13 index12 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number14 index13 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Test</code></div><div class="line number15 index14 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">test_creditBank_all() {</code></div><div class="line number16 index15 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">Map&lt;String,Object&gt; head = </code><code class="java keyword">new</code> <code class="java plain">HashMap&lt;&gt;();</code></div><div class="line number17 index16 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">head.put(</code><code class="java string">"type"</code><code class="java plain">, </code><code class="java string">"cash"</code><code class="java plain">);</code></div><div class="line number18 index17 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">head.put(</code><code class="java string">"aging"</code><code class="java plain">, </code><code class="java string">"fast"</code><code class="java plain">);</code></div><div class="line number19 index18 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sender.creditBank(head, </code><code class="java string">"银行授信(全部匹配)"</code><code class="java plain">);</code></div><div class="line number20 index19 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number21 index20 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number22 index21 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Test</code></div><div class="line number23 index22 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">test_creditFinance_type() {</code></div><div class="line number24 index23 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">Map&lt;String,Object&gt; head = </code><code class="java keyword">new</code> <code class="java plain">HashMap&lt;&gt;();</code></div><div class="line number25 index24 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">head.put(</code><code class="java string">"type"</code><code class="java plain">, </code><code class="java string">"cash"</code><code class="java plain">);</code></div><div class="line number26 index25 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sender.creditFinance(head, </code><code class="java string">"金融公司授信(部分匹配)"</code><code class="java plain">);</code></div><div class="line number27 index26 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number28 index27 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number29 index28 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Test</code></div><div class="line number30 index29 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">test_creditFinance_all() {</code></div><div class="line number31 index30 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">Map&lt;String,Object&gt; head = </code><code class="java keyword">new</code> <code class="java plain">HashMap&lt;&gt;();</code></div><div class="line number32 index31 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">head.put(</code><code class="java string">"type"</code><code class="java plain">, </code><code class="java string">"cash"</code><code class="java plain">);</code></div><div class="line number33 index32 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">head.put(</code><code class="java string">"aging"</code><code class="java plain">, </code><code class="java string">"fast"</code><code class="java plain">);</code></div><div class="line number34 index33 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sender.creditFinance(head, </code><code class="java string">"金融公司授信(全部匹配)"</code><code class="java plain">);</code></div><div class="line number35 index34 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number36 index35 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>4.5）执行ApiCreditSenderTests测试类</p>
<p>生产者日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_457511" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:32:18.954&nbsp; INFO 5204 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.ApiCreditSenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Starting ApiCreditSenderTests on LAPTOP-1DF7S904 with PID 5204 (started by lianjinsoft in...</code></div><div class="line number2 index1 alt1"><code class="html plain">2018-05-14 16:32:18.964&nbsp; INFO 5204 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.ApiCreditSenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : No active profile set, falling back to default profiles: default</code></div><div class="line number3 index2 alt2"><code class="html plain">2018-05-14 16:32:19.007&nbsp; INFO 5204 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1e800aaa: ...</code></div><div class="line number4 index3 alt1"><code class="html plain">2018-05-14 16:32:19.609&nbsp; INFO 5204 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type ...</code></div><div class="line number5 index4 alt2"><code class="html plain">2018-05-14 16:32:20.437&nbsp; INFO 5204 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Starting beans in phase 2147483647</code></div><div class="line number6 index5 alt1"><code class="html plain">2018-05-14 16:32:20.446&nbsp; INFO 5204 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.ApiCreditSenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Started ApiCreditSenderTests in 1.839 seconds (JVM running for 2.759)</code></div><div class="line number7 index6 alt2"><code class="html plain">2018-05-14 16:32:20.566&nbsp; INFO 5204 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : credit.bank send message: 银行授信(部分匹配)</code></div><div class="line number8 index7 alt1"><code class="html plain">2018-05-14 16:32:20.574&nbsp; INFO 5204 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Attempting to connect to: [192.168.15.131:5672]</code></div><div class="line number9 index8 alt2"><code class="html plain">2018-05-14 16:32:20.666&nbsp; INFO 5204 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Created new connection: rabbitConnectionFactory#56ace400:0/SimpleConnection@773cbf4f...</code></div><div class="line number10 index9 alt1"><code class="html plain">2018-05-14 16:32:21.064&nbsp; INFO 5204 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : credit.finance send message: 金融公司授信(全部匹配)</code></div><div class="line number11 index10 alt2"><code class="html plain">2018-05-14 16:32:21.070&nbsp; INFO 5204 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : credit.bank send message: 银行授信(全部匹配)</code></div><div class="line number12 index11 alt1"><code class="html plain">2018-05-14 16:32:21.077&nbsp; INFO 5204 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : credit.finance send message: 金融公司授信(部分匹配)</code></div><div class="line number13 index12 alt2"><code class="html plain">2018-05-14 16:32:21.109&nbsp; INFO 5204 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1e800aaa: ...</code></div><div class="line number14 index13 alt1"><code class="html plain">2018-05-14 16:32:21.114&nbsp; INFO 5204 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Stopping beans in phase 2147483647</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>消费者日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_979290" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:32:21.093&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : credit.finance receive message: 金融公司授信(全部匹配)</code></div><div class="line number2 index1 alt1"><code class="html plain">2018-05-14 16:32:21.094&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : credit.bank receive message: 银行授信(全部匹配)</code></div><div class="line number3 index2 alt2"><code class="html plain">2018-05-14 16:32:21.097&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : credit.finance receive message: 金融公司授信(部分匹配)</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>分析日志：</p>
<p>通过生产者日志第7、10、11、12可以看出，测试的4个方法均已成功发送消息。</p>
<p>通过消费者日志可以看出，credit.bank监听的队列有一条消息没有接收到。</p>
<p>问题：</p>
<p>为什么ApiCreditSenderTests.test_creditBank_type()发送的消息，没有被处理？</p>
<p>答：因为在HeadersConfig配置类中，creditBankExchange交换机的匹配规则是完全匹配，即header attribute参数必须完成一致。</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>5.交换机——FanoutExchange</strong></p>
<p>FanoutExchange交换机是转发消息到所有绑定队列（广播模式，和routingKey没有关系）。</p>
<p>5.1）添加一个配置类（消费者）</p>
<p>配置一个routingKey为api.report.payment的消息队列并绑定在reportExchange交换机上</p>
<p>配置一个routingKey为api.report.refund的消息队列并绑定在reportExchange交换机上</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_163194" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Configuration</code></div><div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">FanoutConfig {</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Bean</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Queue reportPaymentQueue() {</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java keyword">new</code> <code class="java plain">Queue(</code><code class="java string">"api.report.payment"</code><code class="java plain">);</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Bean</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Queue reportRefundQueue() {</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java keyword">new</code> <code class="java plain">Queue(</code><code class="java string">"api.report.refund"</code><code class="java plain">);</code></div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number13 index12 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Bean</code></div><div class="line number14 index13 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">FanoutExchange reportExchange() {</code></div><div class="line number15 index14 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java keyword">new</code> <code class="java plain">FanoutExchange(</code><code class="java string">"reportExchange"</code><code class="java plain">);</code></div><div class="line number16 index15 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number17 index16 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number18 index17 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Bean</code></div><div class="line number19 index18 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Binding bindingReportPaymentExchange(Queue reportPaymentQueue, FanoutExchange reportExchange) {</code></div><div class="line number20 index19 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java plain">BindingBuilder.bind(reportPaymentQueue).to(reportExchange);</code></div><div class="line number21 index20 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number22 index21 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number23 index22 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Bean</code></div><div class="line number24 index23 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Binding bindingReportRefundExchange(Queue reportRefundQueue, FanoutExchange reportExchange) {</code></div><div class="line number25 index24 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java plain">BindingBuilder.bind(reportRefundQueue).to(reportExchange);</code></div><div class="line number26 index25 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number27 index26 alt2"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>5.2）添加一个消息监听类（消费者）</p>
<p>添加payment()方法，监听routingKey为api.report.payment的队列消息</p>
<p>添加refund()方法，监听routingKey为api.report.refund的队列消息</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_767967" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Component</code></div><div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">ApiReportReceive {</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@RabbitHandler</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@RabbitListener</code><code class="java plain">(queues = </code><code class="java string">"api.report.payment"</code><code class="java plain">)</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">payment(String msg) {</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"api.report.payment receive message: "</code><code class="java plain">+msg);</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@RabbitHandler</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@RabbitListener</code><code class="java plain">(queues = </code><code class="java string">"api.report.refund"</code><code class="java plain">)</code></div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">refund(String msg) {</code></div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"api.report.refund receive message: "</code><code class="java plain">+msg);</code></div><div class="line number13 index12 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number14 index13 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>5.3）添加一个消息发送类（生产者）</p>
<p>添加一个generateReports()方法，发送消息至reportExchange交换机</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_925567" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Component</code></div><div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">ApiReportSender {</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Autowired</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java plain">AmqpTemplate rabbitTemplate;</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">generateReports(String msg){</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"api.generate.reports send message: "</code><code class="java plain">+msg);</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">rabbitTemplate.convertAndSend(</code><code class="java string">"reportExchange"</code><code class="java plain">, </code><code class="java string">"api.generate.reports"</code><code class="java plain">, msg);</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number10 index9 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>5.4）添加一个测试类（生产者）</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_756486" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@RunWith</code><code class="java plain">(SpringRunner.</code><code class="java keyword">class</code><code class="java plain">)</code></div><div class="line number2 index1 alt1"><code class="java color1">@SpringBootTest</code></div><div class="line number3 index2 alt2"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">ApiReportSenderTests {</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Autowired</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java plain">ApiReportSender sender;</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Test</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">test_generateReports() {</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sender.generateReports(</code><code class="java string">"开始生成报表！"</code><code class="java plain">);</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number11 index10 alt2"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>5.5）执行ApiReportSenderTests测试类</p>
<p>生产者日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_535721" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:33:41.453&nbsp; INFO 14356 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.ApiReportSenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Starting ApiReportSenderTests on LAPTOP-1DF7S904 with PID 14356 (started by lianjinsoft in ...</code></div><div class="line number2 index1 alt1"><code class="html plain">2018-05-14 16:33:41.454&nbsp; INFO 14356 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.ApiReportSenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : No active profile set, falling back to default profiles: default</code></div><div class="line number3 index2 alt2"><code class="html plain">2018-05-14 16:33:41.490&nbsp; INFO 14356 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1e800aaa: ...</code></div><div class="line number4 index3 alt1"><code class="html plain">2018-05-14 16:33:42.094&nbsp; INFO 14356 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type...</code></div><div class="line number5 index4 alt2"><code class="html plain">2018-05-14 16:33:42.960&nbsp; INFO 14356 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Starting beans in phase 2147483647</code></div><div class="line number6 index5 alt1"><code class="html plain">2018-05-14 16:33:42.972&nbsp; INFO 14356 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.ApiReportSenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Started ApiReportSenderTests in 1.939 seconds (JVM running for 2.843)</code></div><div class="line number7 index6 alt2"><code class="html plain">2018-05-14 16:33:43.037&nbsp; INFO 14356 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : api.generate.reports send message: 开始生成报表！</code></div><div class="line number8 index7 alt1"><code class="html plain">2018-05-14 16:33:43.054&nbsp; INFO 14356 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Attempting to connect to: [192.168.15.131:5672]</code></div><div class="line number9 index8 alt2"><code class="html plain">2018-05-14 16:33:43.174&nbsp; INFO 14356 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Created new connection: rabbitConnectionFactory#56ace400:0/SimpleConnection@773cbf4f...</code></div><div class="line number10 index9 alt1"><code class="html plain">2018-05-14 16:33:43.237&nbsp; INFO 14356 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1e800aaa:...</code></div><div class="line number11 index10 alt2"><code class="html plain">2018-05-14 16:33:43.240&nbsp; INFO 14356 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Stopping beans in phase 2147483647</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>消费者日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_511257" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:33:43.205&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : api.report.payment receive message: 开始生成报表！</code></div><div class="line number2 index1 alt1"><code class="html plain">2018-05-14 16:33:43.207&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : api.report.refund receive message: 开始生成报表！</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>分析日志：</p>
<p>通过生产者日志第7行可以看出，消息已发送成功。</p>
<p>通过消费者日志可以看出，api.report.payment和api.report.refund队列均收到了同一个消息。</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>6.多对一</strong></p>
<p>在实际项目中，我们的系统通常会做集群、分布式或灾备部署。那么就会出现一对多、多对一或多对多的场景。</p>
<p>那么咱们本地如何模拟多对一呢？</p>
<p>6.1）为了测试方便，我们复用PaymentNotifyReceive案例，在PaymentNotifySenderTests测试类中，增加测试方法</p>
<p>test_sender_many2one_1：请求参数为偶数</p>
<p>test_sender_many2one_2：请求参数为奇数</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_301938" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Test</code></div><div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">test_sender_many2one_1() </code><code class="java keyword">throws</code> <code class="java plain">Exception {</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">for</code> <code class="java plain">(</code><code class="java keyword">int</code> <code class="java plain">i = </code><code class="java value">0</code><code class="java plain">; i &lt; </code><code class="java value">20</code><code class="java plain">; i+=</code><code class="java value">2</code><code class="java plain">) {</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sender.sender(</code><code class="java string">"支付订单号："</code><code class="java plain">+i);</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">Thread.sleep(</code><code class="java value">1000</code><code class="java plain">);</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number7 index6 alt2"><code class="java plain">}</code></div><div class="line number8 index7 alt1">&nbsp;</div><div class="line number9 index8 alt2"><code class="java color1">@Test</code></div><div class="line number10 index9 alt1"><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">test_sender_many2one_2() </code><code class="java keyword">throws</code> <code class="java plain">Exception {</code></div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">for</code> <code class="java plain">(</code><code class="java keyword">int</code> <code class="java plain">i = </code><code class="java value">1</code><code class="java plain">; i &lt; </code><code class="java value">20</code><code class="java plain">; i+=</code><code class="java value">2</code><code class="java plain">) {</code></div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sender.sender(</code><code class="java string">"支付订单号："</code><code class="java plain">+i);</code></div><div class="line number13 index12 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">Thread.sleep(</code><code class="java value">1000</code><code class="java plain">);</code></div><div class="line number14 index13 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number15 index14 alt2"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>6.2）执行test_sender_many2one_1()、test_sender_many2one_2()方法</p>
<p>生产者1日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_903530" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:34:49.249&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.PaymentNotifySenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Starting PaymentNotifySenderTests on LAPTOP-1DF7S904 with PID 5064 (started by lianjinsoft in...</code></div><div class="line number2 index1 alt1"><code class="html plain">2018-05-14 16:34:49.250&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.PaymentNotifySenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : No active profile set, falling back to default profiles: default</code></div><div class="line number3 index2 alt2"><code class="html plain">2018-05-14 16:34:49.297&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7eac9008:...</code></div><div class="line number4 index3 alt1"><code class="html plain">2018-05-14 16:34:49.989&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type ...</code></div><div class="line number5 index4 alt2"><code class="html plain">2018-05-14 16:34:51.267&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Starting beans in phase 2147483647</code></div><div class="line number6 index5 alt1"><code class="html plain">2018-05-14 16:34:51.293&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.PaymentNotifySenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Started PaymentNotifySenderTests in 2.449 seconds (JVM running for 3.366)</code></div><div class="line number7 index6 alt2"><code class="html plain">2018-05-14 16:34:51.357&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：0</code></div><div class="line number8 index7 alt1"><code class="html plain">2018-05-14 16:34:51.370&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Attempting to connect to: [192.168.15.131:5672]</code></div><div class="line number9 index8 alt2"><code class="html plain">2018-05-14 16:34:51.817&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Created new connection: rabbitConnectionFactory#56ace400:0/SimpleConnection@773cbf4f ...</code></div><div class="line number10 index9 alt1"><code class="html plain">2018-05-14 16:34:52.866&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：2</code></div><div class="line number11 index10 alt2"><code class="html plain">2018-05-14 16:34:53.870&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：4</code></div><div class="line number12 index11 alt1"><code class="html plain">2018-05-14 16:34:54.870&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：6</code></div><div class="line number13 index12 alt2"><code class="html plain">2018-05-14 16:34:55.871&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：8</code></div><div class="line number14 index13 alt1"><code class="html plain">2018-05-14 16:34:56.872&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：10</code></div><div class="line number15 index14 alt2"><code class="html plain">2018-05-14 16:34:57.872&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：12</code></div><div class="line number16 index15 alt1"><code class="html plain">2018-05-14 16:34:58.873&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：14</code></div><div class="line number17 index16 alt2"><code class="html plain">2018-05-14 16:34:59.875&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：16</code></div><div class="line number18 index17 alt1"><code class="html plain">2018-05-14 16:35:00.876&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：18</code></div><div class="line number19 index18 alt2"><code class="html plain">2018-05-14 16:35:01.882&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7eac9008:...</code></div><div class="line number20 index19 alt1"><code class="html plain">2018-05-14 16:35:01.883&nbsp; INFO 5064 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Stopping beans in phase 2147483647</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>生产者2日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_690221" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:34:52.689&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.PaymentNotifySenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Starting PaymentNotifySenderTests on LAPTOP-1DF7S904 with PID 13988 (started by lianjinsoft in...</code></div><div class="line number2 index1 alt1"><code class="html plain">2018-05-14 16:34:52.690&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.PaymentNotifySenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : No active profile set, falling back to default profiles: default</code></div><div class="line number3 index2 alt2"><code class="html plain">2018-05-14 16:34:52.738&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7eac9008: ...</code></div><div class="line number4 index3 alt1"><code class="html plain">2018-05-14 16:34:53.444&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type ...</code></div><div class="line number5 index4 alt2"><code class="html plain">2018-05-14 16:34:54.567&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Starting beans in phase 2147483647</code></div><div class="line number6 index5 alt1"><code class="html plain">2018-05-14 16:34:54.575&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.PaymentNotifySenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Started PaymentNotifySenderTests in 2.237 seconds (JVM running for 4.18)</code></div><div class="line number7 index6 alt2"><code class="html plain">2018-05-14 16:34:54.788&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：1</code></div><div class="line number8 index7 alt1"><code class="html plain">2018-05-14 16:34:54.796&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Attempting to connect to: [192.168.15.131:5672]</code></div><div class="line number9 index8 alt2"><code class="html plain">2018-05-14 16:34:54.870&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Created new connection: rabbitConnectionFactory#305f7627:0/SimpleConnection@665e9289...</code></div><div class="line number10 index9 alt1"><code class="html plain">2018-05-14 16:34:55.903&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：3</code></div><div class="line number11 index10 alt2"><code class="html plain">2018-05-14 16:34:56.903&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：5</code></div><div class="line number12 index11 alt1"><code class="html plain">2018-05-14 16:34:57.904&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：7</code></div><div class="line number13 index12 alt2"><code class="html plain">2018-05-14 16:34:58.904&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：9</code></div><div class="line number14 index13 alt1"><code class="html plain">2018-05-14 16:34:59.905&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：11</code></div><div class="line number15 index14 alt2"><code class="html plain">2018-05-14 16:35:00.906&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：13</code></div><div class="line number16 index15 alt1"><code class="html plain">2018-05-14 16:35:01.907&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：15</code></div><div class="line number17 index16 alt2"><code class="html plain">2018-05-14 16:35:02.907&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：17</code></div><div class="line number18 index17 alt1"><code class="html plain">2018-05-14 16:35:03.910&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：19</code></div><div class="line number19 index18 alt2"><code class="html plain">2018-05-14 16:35:04.928&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7eac9008:...</code></div><div class="line number20 index19 alt1"><code class="html plain">2018-05-14 16:35:04.932&nbsp; INFO 13988 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Stopping beans in phase 2147483647</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>消费者日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_176147" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:34:51.853&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：0</code></div><div class="line number2 index1 alt1"><code class="html plain">2018-05-14 16:34:52.871&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：2</code></div><div class="line number3 index2 alt2"><code class="html plain">2018-05-14 16:34:53.871&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：4</code></div><div class="line number4 index3 alt1"><code class="html plain">2018-05-14 16:34:54.871&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：6</code></div><div class="line number5 index4 alt2"><code class="html plain">2018-05-14 16:34:54.902&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：1</code></div><div class="line number6 index5 alt1"><code class="html plain">2018-05-14 16:34:55.872&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：8</code></div><div class="line number7 index6 alt2"><code class="html plain">2018-05-14 16:34:55.904&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：3</code></div><div class="line number8 index7 alt1"><code class="html plain">2018-05-14 16:34:56.873&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：10</code></div><div class="line number9 index8 alt2"><code class="html plain">2018-05-14 16:34:56.905&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：5</code></div><div class="line number10 index9 alt1"><code class="html plain">2018-05-14 16:34:57.873&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：12</code></div><div class="line number11 index10 alt2"><code class="html plain">2018-05-14 16:34:57.905&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：7</code></div><div class="line number12 index11 alt1"><code class="html plain">2018-05-14 16:34:58.878&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：14</code></div><div class="line number13 index12 alt2"><code class="html plain">2018-05-14 16:34:58.906&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：9</code></div><div class="line number14 index13 alt1"><code class="html plain">2018-05-14 16:34:59.877&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：16</code></div><div class="line number15 index14 alt2"><code class="html plain">2018-05-14 16:34:59.906&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：11</code></div><div class="line number16 index15 alt1"><code class="html plain">2018-05-14 16:35:00.877&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：18</code></div><div class="line number17 index16 alt2"><code class="html plain">2018-05-14 16:35:00.909&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：13</code></div><div class="line number18 index17 alt1"><code class="html plain">2018-05-14 16:35:01.909&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：15</code></div><div class="line number19 index18 alt2"><code class="html plain">2018-05-14 16:35:02.911&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：17</code></div><div class="line number20 index19 alt1"><code class="html plain">2018-05-14 16:35:03.914&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：19</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>分析日志：</p>
<p>从生产者1、生产者2日志中可以看出，所有消息均已经发送成功。</p>
<p>从消费者日志可以看出，所有消息均被成功接收处理。</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>7.一对多</strong></p>
<p>如何模拟一对多？</p>
<p>7.1）为了测试方便，我们继续复用PaymentNotifyReceive案例，在PaymentNotifySenderTests测试类中，增加测试方法</p>
<p>test_sender_one2many：循环调用20次。</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_214642" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Test</code></div><div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">test_sender_one2many() {</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">for</code> <code class="java plain">(</code><code class="java keyword">int</code> <code class="java plain">i = </code><code class="java value">0</code><code class="java plain">; i &lt; </code><code class="java value">20</code><code class="java plain">; i++) {</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sender.sender(</code><code class="java string">"支付订单号："</code><code class="java plain">+i);</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number6 index5 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>7.2）测试</p>
<p>为了达到一对多的效果，我们需要多启动一个（或多个）消费者。然后执行test_sender_one2many()测试方法。</p>
<p>生产者日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_54113" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div><div class="line number28 index27 alt1">28</div><div class="line number29 index28 alt2">29</div><div class="line number30 index29 alt1">30</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:36:27.703&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.PaymentNotifySenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Starting PaymentNotifySenderTests on LAPTOP-1DF7S904 with PID 7508 (started by lianjinsoft in...</code></div><div class="line number2 index1 alt1"><code class="html plain">2018-05-14 16:36:27.704&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.PaymentNotifySenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : No active profile set, falling back to default profiles: default</code></div><div class="line number3 index2 alt2"><code class="html plain">2018-05-14 16:36:27.729&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7eac9008:...</code></div><div class="line number4 index3 alt1"><code class="html plain">2018-05-14 16:36:28.391&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type...</code></div><div class="line number5 index4 alt2"><code class="html plain">2018-05-14 16:36:29.285&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Starting beans in phase 2147483647</code></div><div class="line number6 index5 alt1"><code class="html plain">2018-05-14 16:36:29.303&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.PaymentNotifySenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Started PaymentNotifySenderTests in 2.197 seconds (JVM running for 3.097)</code></div><div class="line number7 index6 alt2"><code class="html plain">2018-05-14 16:36:29.504&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：0</code></div><div class="line number8 index7 alt1"><code class="html plain">2018-05-14 16:36:29.516&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Attempting to connect to: [192.168.15.131:5672]</code></div><div class="line number9 index8 alt2"><code class="html plain">2018-05-14 16:36:29.635&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Created new connection: rabbitConnectionFactory#4f071df8:0/SimpleConnection@42a9e5d1...</code></div><div class="line number10 index9 alt1"><code class="html plain">2018-05-14 16:36:29.672&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：1</code></div><div class="line number11 index10 alt2"><code class="html plain">2018-05-14 16:36:29.679&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：2</code></div><div class="line number12 index11 alt1"><code class="html plain">2018-05-14 16:36:29.705&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：3</code></div><div class="line number13 index12 alt2"><code class="html plain">2018-05-14 16:36:29.707&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：4</code></div><div class="line number14 index13 alt1"><code class="html plain">2018-05-14 16:36:29.710&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：5</code></div><div class="line number15 index14 alt2"><code class="html plain">2018-05-14 16:36:29.710&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：6</code></div><div class="line number16 index15 alt1"><code class="html plain">2018-05-14 16:36:29.710&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：7</code></div><div class="line number17 index16 alt2"><code class="html plain">2018-05-14 16:36:29.713&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：8</code></div><div class="line number18 index17 alt1"><code class="html plain">2018-05-14 16:36:29.719&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：9</code></div><div class="line number19 index18 alt2"><code class="html plain">2018-05-14 16:36:29.728&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：10</code></div><div class="line number20 index19 alt1"><code class="html plain">2018-05-14 16:36:29.729&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：11</code></div><div class="line number21 index20 alt2"><code class="html plain">2018-05-14 16:36:29.733&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：12</code></div><div class="line number22 index21 alt1"><code class="html plain">2018-05-14 16:36:29.734&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：13</code></div><div class="line number23 index22 alt2"><code class="html plain">2018-05-14 16:36:29.734&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：14</code></div><div class="line number24 index23 alt1"><code class="html plain">2018-05-14 16:36:29.734&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：15</code></div><div class="line number25 index24 alt2"><code class="html plain">2018-05-14 16:36:29.741&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：16</code></div><div class="line number26 index25 alt1"><code class="html plain">2018-05-14 16:36:29.742&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：17</code></div><div class="line number27 index26 alt2"><code class="html plain">2018-05-14 16:36:29.742&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：18</code></div><div class="line number28 index27 alt1"><code class="html plain">2018-05-14 16:36:29.744&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment send message: 支付订单号：19</code></div><div class="line number29 index28 alt2"><code class="html plain">2018-05-14 16:36:29.762&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7eac9008:...</code></div><div class="line number30 index29 alt1"><code class="html plain">2018-05-14 16:36:29.767&nbsp; INFO 7508 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Stopping beans in phase 2147483647</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>消费者1日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_435324" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:36:29.675&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：0</code></div><div class="line number2 index1 alt1"><code class="html plain">2018-05-14 16:36:29.690&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：2</code></div><div class="line number3 index2 alt2"><code class="html plain">2018-05-14 16:36:29.712&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：4</code></div><div class="line number4 index3 alt1"><code class="html plain">2018-05-14 16:36:29.714&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：6</code></div><div class="line number5 index4 alt2"><code class="html plain">2018-05-14 16:36:29.728&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：9</code></div><div class="line number6 index5 alt1"><code class="html plain">2018-05-14 16:36:29.733&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：11</code></div><div class="line number7 index6 alt2"><code class="html plain">2018-05-14 16:36:29.737&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：13</code></div><div class="line number8 index7 alt1"><code class="html plain">2018-05-14 16:36:29.740&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：15</code></div><div class="line number9 index8 alt2"><code class="html plain">2018-05-14 16:36:29.743&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：16</code></div><div class="line number10 index9 alt1"><code class="html plain">2018-05-14 16:36:29.745&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：18</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>消费者2日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_70070" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:36:29.705&nbsp; INFO 13448 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：1</code></div><div class="line number2 index1 alt1"><code class="html plain">2018-05-14 16:36:29.709&nbsp; INFO 13448 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：3</code></div><div class="line number3 index2 alt2"><code class="html plain">2018-05-14 16:36:29.712&nbsp; INFO 13448 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：5</code></div><div class="line number4 index3 alt1"><code class="html plain">2018-05-14 16:36:29.718&nbsp; INFO 13448 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：7</code></div><div class="line number5 index4 alt2"><code class="html plain">2018-05-14 16:36:29.722&nbsp; INFO 13448 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：8</code></div><div class="line number6 index5 alt1"><code class="html plain">2018-05-14 16:36:29.731&nbsp; INFO 13448 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：10</code></div><div class="line number7 index6 alt2"><code class="html plain">2018-05-14 16:36:29.736&nbsp; INFO 13448 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：12</code></div><div class="line number8 index7 alt1"><code class="html plain">2018-05-14 16:36:29.738&nbsp; INFO 13448 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：14</code></div><div class="line number9 index8 alt2"><code class="html plain">2018-05-14 16:36:29.744&nbsp; INFO 13448 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：17</code></div><div class="line number10 index9 alt1"><code class="html plain">2018-05-14 16:36:29.747&nbsp; INFO 13448 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.payment receive message: 支付订单号：19</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>分析日志：</p>
<p>从生产者日志可以看出，所有的消息均已经发送成功。</p>
<p>从消费者1、消费者2日志可以看出，消息被两个消费者均衡消费了。</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>8.发送对象</strong></p>
<p>实际项目中，请求信息可能包含多个字段。为了保证生产者与消费者两端的字段一致性，通常会传递一个对象。</p>
<p>8.1）为了测试方便，我们在DirectConfig中增加一个消息队列</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_346425" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Bean</code></div><div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java plain">Queue refundNotifyQueue() {</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java keyword">new</code> <code class="java plain">Queue(</code><code class="java string">"notify.refund"</code><code class="java plain">);</code></div><div class="line number4 index3 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>8.2）添加一个消息监听类（消费者）</p>
<p>监听routingKey为notify.refund的队列消息</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_577267" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Component</code></div><div class="line number2 index1 alt1"><code class="java color1">@RabbitListener</code><code class="java plain">(queues = </code><code class="java string">"notify.refund"</code><code class="java plain">)</code></div><div class="line number3 index2 alt2"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">RefundNotifyReceive {</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@RabbitHandler</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">receive(Order order) {</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"notify.refund receive message: "</code><code class="java plain">+order);</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number8 index7 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>8.3）添加一个消息发送类（生产者）</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_255840" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Component</code></div><div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">RefundNotifySender {</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Autowired</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java plain">AmqpTemplate rabbitTemplate;</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">sender(Order order){</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"notify.refund send message: "</code><code class="java plain">+order);</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">rabbitTemplate.convertAndSend(</code><code class="java string">"notify.refund"</code><code class="java plain">, order);</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number10 index9 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>8.4）添加一个测试类</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_314523" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@RunWith</code><code class="java plain">(SpringRunner.</code><code class="java keyword">class</code><code class="java plain">)</code></div><div class="line number2 index1 alt1"><code class="java color1">@SpringBootTest</code></div><div class="line number3 index2 alt2"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">RefundNotifySenderTests {</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Autowired</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java plain">RefundNotifySender sender;</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Test</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">test_sender() {</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">Order order = </code><code class="java keyword">new</code> <code class="java plain">Order();</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">order.setId(</code><code class="java value">100001</code><code class="java plain">);</code></div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">order.setOrderId(String.valueOf(System.currentTimeMillis()));</code></div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">order.setAmount(</code><code class="java keyword">new</code> <code class="java plain">BigDecimal(</code><code class="java string">"1999.99"</code><code class="java plain">));</code></div><div class="line number13 index12 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">order.setCreateTime(</code><code class="java keyword">new</code> <code class="java plain">Date());</code></div><div class="line number14 index13 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sender.sender(order);</code></div><div class="line number15 index14 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number16 index15 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>8.5）执行RefundNotifySenderTests测试类</p>
<p>生产者日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_365115" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:37:47.038&nbsp; INFO 13672 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.RefundNotifySenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Starting RefundNotifySenderTests on LAPTOP-1DF7S904 with PID 13672 (started by lianjinsoft in...</code></div><div class="line number2 index1 alt1"><code class="html plain">2018-05-14 16:37:47.041&nbsp; INFO 13672 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.RefundNotifySenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : No active profile set, falling back to default profiles: default</code></div><div class="line number3 index2 alt2"><code class="html plain">2018-05-14 16:37:47.070&nbsp; INFO 13672 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1e800aaa:...</code></div><div class="line number4 index3 alt1"><code class="html plain">2018-05-14 16:37:47.715&nbsp; INFO 13672 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type ...</code></div><div class="line number5 index4 alt2"><code class="html plain">2018-05-14 16:37:48.779&nbsp; INFO 13672 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Starting beans in phase 2147483647</code></div><div class="line number6 index5 alt1"><code class="html plain">2018-05-14 16:37:48.802&nbsp; INFO 13672 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.RefundNotifySenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Started RefundNotifySenderTests in 2.082 seconds (JVM running for 2.967)</code></div><div class="line number7 index6 alt2"><code class="html plain">2018-05-14 16:37:49.085&nbsp; INFO 13672 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.refund send message: Order [id=100001, orderId=1526287069081, amount=1999.99, createTime=...</code></div><div class="line number8 index7 alt1"><code class="html plain">2018-05-14 16:37:49.104&nbsp; INFO 13672 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Attempting to connect to: [192.168.15.131:5672]</code></div><div class="line number9 index8 alt2"><code class="html plain">2018-05-14 16:37:49.170&nbsp; INFO 13672 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Created new connection: rabbitConnectionFactory#6b5894c8:0/SimpleConnection@38f57b3d [delegate=...</code></div><div class="line number10 index9 alt1"><code class="html plain">2018-05-14 16:37:49.265&nbsp; INFO 13672 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1e800aaa: ...</code></div><div class="line number11 index10 alt2"><code class="html plain">2018-05-14 16:37:49.266&nbsp; INFO 13672 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Stopping beans in phase 2147483647</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>消费者日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_85176" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:37:49.242&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.refund receive message: Order [id=100001, orderId=1526287069081, amount=1999.99, createTime=...</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>分析日志：</p>
<p>从生产者日志可以看出，order对象已经发送成功。</p>
<p>从消费者日志可以看出，order对象已经接受成功并可以直接使用。</p>
<p>&nbsp;</p>
<p><strong>注意：</strong></p>
<p><span style="color: #000000;">传递的对象必须支持序列化（实现了Serializable接口）</span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>9.RPC</strong></p>
<p>RabbitMQ支持RPC远程调用，同步返回结果。</p>
<p><img src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/1308658-20180515112959267-1677496947.png" alt=""></p>
<p>9.1）为了测试方便，我们在DirectConfig中增加一个消息队列</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_259601" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Bean</code></div><div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java plain">Queue queryOrderQueue() {</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java keyword">new</code> <code class="java plain">Queue(</code><code class="java string">"query.order"</code><code class="java plain">);</code></div><div class="line number4 index3 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>9.2）添加一个消息监听类（消费者）</p>
<p>监听routingKey为query.order的队列消息</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_179346" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Component</code></div><div class="line number2 index1 alt1"><code class="java color1">@RabbitListener</code><code class="java plain">(queues = </code><code class="java string">"query.order"</code><code class="java plain">)</code></div><div class="line number3 index2 alt2"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">QueryOrderReceive {</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@RabbitHandler</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Order receive(String orderId) {</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"notify.refund receive message: "</code><code class="java plain">+orderId);</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">Order order = </code><code class="java keyword">new</code> <code class="java plain">Order();</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">order.setId(</code><code class="java value">100001</code><code class="java plain">);</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">order.setOrderId(orderId);</code></div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">order.setAmount(</code><code class="java keyword">new</code> <code class="java plain">BigDecimal(</code><code class="java string">"2999.99"</code><code class="java plain">));</code></div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">order.setCreateTime(</code><code class="java keyword">new</code> <code class="java plain">Date());</code></div><div class="line number13 index12 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java plain">order;</code></div><div class="line number14 index13 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number15 index14 alt2"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>9.3）添加一个消息发送类（生产者）</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_549551" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@Component</code></div><div class="line number2 index1 alt1"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">QueryOrderSender {</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Autowired</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java plain">AmqpTemplate rabbitTemplate;</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">sender(String orderId){</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"query.order send message: "</code><code class="java plain">+orderId);</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">Order order = (Order) rabbitTemplate.convertSendAndReceive(</code><code class="java string">"query.order"</code><code class="java plain">, orderId);</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">LogUtil.info(</code><code class="java string">"query.order return message: "</code><code class="java plain">+order);</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number11 index10 alt2"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>9.4）添加一个测试类</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_672654" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java color1">@RunWith</code><code class="java plain">(SpringRunner.</code><code class="java keyword">class</code><code class="java plain">)</code></div><div class="line number2 index1 alt1"><code class="java color1">@SpringBootTest</code></div><div class="line number3 index2 alt2"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">QueryOrderSenderTests {</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Autowired</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java plain">QueryOrderSender sender;</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java color1">@Test</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">test_sender() {</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">sender.sender(</code><code class="java string">"900000001"</code><code class="java plain">);</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number11 index10 alt2"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>　　</p>
<p>9.5）执行QueryOrderSenderTests测试类</p>
<p>生产者日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_797537" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:38:14.163&nbsp; INFO 2024 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.QueryOrderSenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Starting QueryOrderSenderTests on LAPTOP-1DF7S904 with PID 2024 (started by lianjinsoft in...</code></div><div class="line number2 index1 alt1"><code class="html plain">2018-05-14 16:38:14.164&nbsp; INFO 2024 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.QueryOrderSenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : No active profile set, falling back to default profiles: default</code></div><div class="line number3 index2 alt2"><code class="html plain">2018-05-14 16:38:14.197&nbsp; INFO 2024 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7eac9008: ...</code></div><div class="line number4 index3 alt1"><code class="html plain">2018-05-14 16:38:14.848&nbsp; INFO 2024 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type ...</code></div><div class="line number5 index4 alt2"><code class="html plain">2018-05-14 16:38:15.705&nbsp; INFO 2024 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Starting beans in phase 2147483647</code></div><div class="line number6 index5 alt1"><code class="html plain">2018-05-14 16:38:15.715&nbsp; INFO 2024 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] c.l.sender.QueryOrderSenderTests&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Started QueryOrderSenderTests in 1.927 seconds (JVM running for 3.079)</code></div><div class="line number7 index6 alt2"><code class="html plain">2018-05-14 16:38:15.793&nbsp; INFO 2024 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : query.order send message: 900000001</code></div><div class="line number8 index7 alt1"><code class="html plain">2018-05-14 16:38:15.812&nbsp; INFO 2024 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Attempting to connect to: [192.168.15.131:5672]</code></div><div class="line number9 index8 alt2"><code class="html plain">2018-05-14 16:38:15.988&nbsp; INFO 2024 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] o.s.a.r.c.CachingConnectionFactory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Created new connection: rabbitConnectionFactory#38be305c:0/SimpleConnection@71984c3 ...</code></div><div class="line number10 index9 alt1"><code class="html plain">2018-05-14 16:38:16.057&nbsp; INFO 2024 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : query.order return message: Order [id=100001, orderId=900000001, amount=2999.99, createTime=...</code></div><div class="line number11 index10 alt2"><code class="html plain">2018-05-14 16:38:16.079&nbsp; INFO 2024 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7eac9008: ...</code></div><div class="line number12 index11 alt1"><code class="html plain">2018-05-14 16:38:16.097&nbsp; INFO 2024 --- [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread-2] o.s.c.support.DefaultLifecycleProcessor&nbsp; : Stopping beans in phase 2147483647</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>消费者日志：</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_314305" class="syntaxhighlighter  html"><div class="toolbar"><span><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="html plain">2018-05-14 16:38:16.028&nbsp; INFO 14876 --- [cTaskExecutor-1] com.lianjinsoft.util.LogUtil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : notify.refund receive message: 900000001</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>分析日志：</p>
<p>从生产者日志第7行可以看出，消息已经发送成功。从第10行日志可以看出，已经收取到返回的消息，并成功转化为Order对象。</p>
<p>从消费者日志可以看出，已经成功接收到消息并处理完成。</p>
<p>&nbsp;</p>
<p>虽然RabbitMQ支持RPC接口调用，但不推荐使用。</p>
<p>原因：</p>
<p>1）RPC默认为单线程阻塞模型，效率极低。</p>
<p>2）需要手动实现多线程消费。</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>安装RabbitMQ请参考：<a id="homepage1_HomePageDays_DaysList_ctl00_DayList_TitleUrl_0" href="http://www.cnblogs.com/skychenjiajun/p/8930147.html">CentOS在线安装RabbitMQ3.7</a></p>
<p>本帖源代码：https://gitee.com/skychenjiajun/spring-boot</p>
<p>&nbsp;</p></div><div id="MySignature"></div>
<div class="clear"></div>
<div id="blog_post_info_block">
<div id="BlogPostCategory">分类: <a href="https://www.cnblogs.com/skychenjiajun/category/1201353.html" target="_blank">Java</a>,<a href="https://www.cnblogs.com/skychenjiajun/category/1205328.html" target="_blank">MQ</a>,<a href="https://www.cnblogs.com/skychenjiajun/category/1139375.html" target="_blank">Spring Boot</a></div>
<div id="EntryTag">标签: <a href="https://www.cnblogs.com/skychenjiajun/tag/springboot/">springboot</a>, <a href="https://www.cnblogs.com/skychenjiajun/tag/RabbitMQ/">RabbitMQ</a>, <a href="https://www.cnblogs.com/skychenjiajun/tag/mq/">mq</a></div>
<div id="blog_post_info"><div id="green_channel">
        <a href="javascript:void(0);" id="green_channel_digg" onclick="DiggIt(9037324,cb_blogId,1);green_channel_success(this,&#39;谢谢推荐！&#39;);">好文要顶</a>
            <a id="green_channel_follow" onclick="follow(&#39;ae035141-3266-4814-17e3-08d54dba4453&#39;);" href="javascript:void(0);">关注我</a>
    <a id="green_channel_favorite" onclick="AddToWz(cb_entryId);return false;" href="javascript:void(0);">收藏该文</a>
    <a id="green_channel_weibo" href="javascript:void(0);" title="分享至新浪微博" onclick="ShareToTsina()"><img src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/icon_weibo_24.png" alt=""></a>
    <a id="green_channel_wechat" href="javascript:void(0);" title="分享至微信" onclick="shareOnWechat()"><img src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/wechat.png" alt=""></a>
</div>
<div id="author_profile">
    <div id="author_profile_info" class="author_profile_info">
            <a href="http://home.cnblogs.com/u/skychenjiajun/" target="_blank"><img src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/20171229094846.png" class="author_avatar" alt=""></a>
        <div id="author_profile_detail" class="author_profile_info">
            <a href="http://home.cnblogs.com/u/skychenjiajun/">名山丶深处</a><br>
            <a href="http://home.cnblogs.com/u/skychenjiajun/followees">关注 - 1</a><br>
            <a href="http://home.cnblogs.com/u/skychenjiajun/followers">粉丝 - 3</a>
        </div>
    </div>
    <div class="clear"></div>
    <div id="author_profile_honor"></div>
    <div id="author_profile_follow">
                <a href="javascript:void(0);" onclick="follow(&#39;ae035141-3266-4814-17e3-08d54dba4453&#39;);return false;">+加关注</a>
    </div>
</div>
<div id="div_digg">
    <div class="diggit" onclick="votePost(9037324,&#39;Digg&#39;)">
        <span class="diggnum" id="digg_count">3</span>
    </div>
    <div class="buryit" onclick="votePost(9037324,&#39;Bury&#39;)">
        <span class="burynum" id="bury_count">0</span>
    </div>
    <div class="clear"></div>
    <div class="diggword" id="digg_tips">
    </div>
</div>
<script type="text/javascript">
    currentDiggType = 0;
</script></div>
<div class="clear"></div>
<div id="post_next_prev"><a href="https://www.cnblogs.com/skychenjiajun/p/8930147.html" class="p_n_p_prefix">« </a> 上一篇：<a href="https://www.cnblogs.com/skychenjiajun/p/8930147.html" title="发布于2018-04-24 17:04">CentOS在线安装RabbitMQ3.7</a><br><a href="https://www.cnblogs.com/skychenjiajun/p/9057379.html" class="p_n_p_prefix">» </a> 下一篇：<a href="https://www.cnblogs.com/skychenjiajun/p/9057379.html" title="发布于2018-05-21 11:46">springboot集成schedule（深度理解）</a><br></div>
</div>


		</div>
		<div class="postDesc">posted @ <span id="post-date">2018-05-15 13:52</span> <a href="https://www.cnblogs.com/skychenjiajun/">名山丶深处</a> 阅读(<span id="post_view_count">5472</span>) 评论(<span id="post_comment_count">4</span>)  <a href="https://i.cnblogs.com/EditPosts.aspx?postid=9037324" rel="nofollow">编辑</a> <a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" onclick="AddToWz(9037324);return false;">收藏</a></div>
	</div>
	<script type="text/javascript">var allowComments=true,cb_blogId=404128,cb_entryId=9037324,cb_blogApp=currentBlogApp,cb_blogUserGuid='ae035141-3266-4814-17e3-08d54dba4453',cb_entryCreatedDate='2018/5/15 13:52:00';loadViewCount(cb_entryId);var cb_postType=1;var isMarkdown=false;</script>
	
</div><!--end: topics 文章、评论容器-->
</div><a name="!comments"></a><div id="blog-comments-placeholder"><div id="comments_pager_top"></div>
<br>
<div class="feedback_area_title">评论列表</div>
<div class="feedbackNoItems"></div>	

		<div class="feedbackItem">
			<div class="feedbackListSubtitle">
				<div class="feedbackManage">
					&nbsp;&nbsp;<span class="comment_actions"></span>
				</div>
				<a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#4016484" class="layer">#1楼</a><a name="4016484" id="comment_anchor_4016484"></a>  <span class="comment_date">2018-07-09 21:04</span> <a id="a_comment_author_4016484" href="https://www.cnblogs.com/fyql/" target="_blank">潜林</a> <a href="http://msg.cnblogs.com/send/%E6%BD%9C%E6%9E%97" title="发送站内短消息" class="sendMsg2This">&nbsp;</a>
			</div>
			<div class="feedbackCon">
				<div id="comment_body_4016484" class="blog_comment_body">总结得很详细，学习了。<br>按照在4.3）添加一个消息发送类（生产者）的 代码，但是 getMessage(head, msg) 方法报错找不到。请问这个方法是哪儿定义的？谢谢！<br>版本：Springboot 2.0.3.RELEASE</div><div class="comment_vote"><a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(4016484,&#39;Digg&#39;,this)">支持(0)</a><a href="javascript:void(0);" class="comment_bury" onclick="return voteComment(4016484,&#39;Bury&#39;,this)">反对(0)</a></div><span id="comment_4016484_avatar" style="display:none;">http://pic.cnblogs.com/face/800902/20181226215148.png</span>
			</div>
		</div>
	
		<div class="feedbackItem">
			<div class="feedbackListSubtitle">
				<div class="feedbackManage">
					&nbsp;&nbsp;<span class="comment_actions"></span>
				</div>
				<a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#4018694" class="layer">#2楼</a><a name="4018694" id="comment_anchor_4018694"></a>[<span class="louzhu">楼主</span>]  <span class="comment_date">2018-07-12 11:11</span> <a id="a_comment_author_4018694" href="https://www.cnblogs.com/skychenjiajun/" target="_blank">名山丶深处</a> <a href="http://msg.cnblogs.com/send/%E5%90%8D%E5%B1%B1%E4%B8%B6%E6%B7%B1%E5%A4%84" title="发送站内短消息" class="sendMsg2This">&nbsp;</a>
			</div>
			<div class="feedbackCon">
				<div id="comment_body_4018694" class="blog_comment_body"><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#4016484" title="查看所回复的评论" onclick="commentManager.renderComments(0,50,4016484);">@</a>
潜林<br><br>这个方法是自己封装的，本帖最后附有源码链接。可以下载查看。<br>方法源码如下：<br>private Message getMessage(Map&lt;String, Object&gt; head, Object msg){<br>		MessageProperties messageProperties = new MessageProperties();<br>		for (Map.Entry&lt;String, Object&gt; entry : head.entrySet()) {<br>			messageProperties.setHeader(entry.getKey(), entry.getValue());<br>		}<br>		MessageConverter messageConverter = new SimpleMessageConverter();<br>		return messageConverter.toMessage(msg, messageProperties);<br>	}</div><div class="comment_vote"><a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(4018694,&#39;Digg&#39;,this)">支持(1)</a><a href="javascript:void(0);" class="comment_bury" onclick="return voteComment(4018694,&#39;Bury&#39;,this)">反对(0)</a></div><span id="comment_4018694_avatar" style="display:none;">http://pic.cnblogs.com/face/1308658/20171229094846.png</span>
			</div>
		</div>
	
		<div class="feedbackItem">
			<div class="feedbackListSubtitle">
				<div class="feedbackManage">
					&nbsp;&nbsp;<span class="comment_actions"></span>
				</div>
				<a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#4132852" class="layer">#3楼</a><a name="4132852" id="comment_anchor_4132852"></a>  <span class="comment_date">2018-12-06 12:26</span> <a id="a_comment_author_4132852" href="http://home.cnblogs.com/u/1067454/" target="_blank">不锈钢奶豆</a> <a href="http://msg.cnblogs.com/send/%E4%B8%8D%E9%94%88%E9%92%A2%E5%A5%B6%E8%B1%86" title="发送站内短消息" class="sendMsg2This">&nbsp;</a>
			</div>
			<div class="feedbackCon">
				<div id="comment_body_4132852" class="blog_comment_body">十分感谢您的分享，学习到了</div><div class="comment_vote"><a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(4132852,&#39;Digg&#39;,this)">支持(0)</a><a href="javascript:void(0);" class="comment_bury" onclick="return voteComment(4132852,&#39;Bury&#39;,this)">反对(0)</a></div>
			</div>
		</div>
	
		<div class="feedbackItem">
			<div class="feedbackListSubtitle">
				<div class="feedbackManage">
					&nbsp;&nbsp;<span class="comment_actions"></span>
				</div>
				<a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#4137644" class="layer">#4楼</a><a name="4137644" id="comment_anchor_4137644"></a><span id="comment-maxId" style="display:none;">4137644</span><span id="comment-maxDate" style="display:none;">2018/12/12 15:29:46</span>  <span class="comment_date">2018-12-12 15:29</span> <a id="a_comment_author_4137644" href="https://www.cnblogs.com/mokongzei/" target="_blank">摩箜贼</a> <a href="http://msg.cnblogs.com/send/%E6%91%A9%E7%AE%9C%E8%B4%BC" title="发送站内短消息" class="sendMsg2This">&nbsp;</a>
			</div>
			<div class="feedbackCon">
				<div id="comment_body_4137644" class="blog_comment_body">写的很详细！！！</div><div class="comment_vote"><a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(4137644,&#39;Digg&#39;,this)">支持(0)</a><a href="javascript:void(0);" class="comment_bury" onclick="return voteComment(4137644,&#39;Bury&#39;,this)">反对(0)</a></div><span id="comment_4137644_avatar" style="display:none;">http://pic.cnblogs.com/face/u383296.jpg?id=08114342</span>
			</div>
		</div>
	<div id="comments_pager_bottom"></div></div><script type="text/javascript">var commentManager = new blogCommentManager();commentManager.renderComments(0);</script>
<div id="comment_form" class="commentform">
<a name="commentform"></a>
<div id="divCommentShow"></div>
<div id="comment_nav"><span id="span_refresh_tips"></span><a href="javascript:void(0);" onclick="return RefreshCommentList();" id="lnk_RefreshComments" runat="server" clientidmode="Static">刷新评论</a><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#" onclick="return RefreshPage();">刷新页面</a><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#top">返回顶部</a></div>
<div id="comment_form_container"><div class="login_tips">注册用户登录后才能发表评论，请 <a rel="nofollow" href="javascript:void(0);" class="underline" onclick="return login(&#39;commentform&#39;);">登录</a> 或 <a rel="nofollow" href="javascript:void(0);" class="underline" onclick="return register();">注册</a>，<a href="http://www.cnblogs.com/">访问</a>网站首页。</div></div>
<div class="ad_text_commentbox" id="ad_text_under_commentbox"></div>
<div id="ad_t2"><a href="https://www.cnblogs.com/cmt/p/10496052.html" target="_blank" onclick="ga(&#39;send&#39;, &#39;event&#39;, &#39;Link&#39;, &#39;click&#39;, &#39;T2-51CTO&#39;)">【课程】开学季给程序员们送福利啦！限量X-box等你来拿！</a><br><a href="http://www.ucancode.com/index.htm" target="_blank" onclick="ga(&#39;send&#39;, &#39;event&#39;, &#39;Link&#39;, &#39;click&#39;, &#39;T2-工控&#39;)">【推荐】超50万C++/C#源码: 大型实时仿真组态图形源码</a><br><a href="https://cloud.baidu.com/campaign/purchase-2019/index.html?track=cp:dsp|pf:pc|pp:chui-bokeyuan-huodong-19caigouji-BCC-neiyeCH-190311|pu:neiyeCH-wenzilian|ci:2019cgj|kw:2105959" target="_blank" onclick="ga(&#39;send&#39;, &#39;event&#39;, &#39;Link&#39;, &#39;click&#39;, &#39;T2-百度云&#39;)">【推荐】百度云“猪”你开年行大运，红包疯狂拿</a><br><a href="https://news.cnblogs.com/n/621139/" target="_blank" onclick="ga(&#39;send&#39;, &#39;event&#39;, &#39;Link&#39;, &#39;click&#39;, &#39;T2-Open Source Summit&#39;)">【活动】2019开源技术盛宴(6.24~26上海世博中心)</a><br><a href="https://ke.qq.com/adActivity.html?name=xiangxueketang2" target="_blank" onclick="ga(&#39;send&#39;, &#39;event&#39;, &#39;Link&#39;, &#39;click&#39;, &#39;T2-享学&#39;)">【推荐】55K刚面完Java架构师岗，这些技术你必须掌握</a><br></div>
<div id="opt_under_post"></div>
<script async="async" src="./springboot集成rabbitmq（实战） - 名山丶深处 - 博客园_files/gpt.js.下载"></script>
<script>
  var googletag = googletag || {};
  googletag.cmd = googletag.cmd || [];
</script>
<script>
  googletag.cmd.push(function() {
        googletag.defineSlot('/1090369/C1', [300, 250], 'div-gpt-ad-1546353474406-0').addService(googletag.pubads());
        googletag.defineSlot('/1090369/C2', [468, 60], 'div-gpt-ad-1539008685004-0').addService(googletag.pubads());
        googletag.pubads().enableSingleRequest();
        googletag.enableServices();
  });
</script>
<div id="cnblogs_c1" class="c_ad_block">
    <div id="div-gpt-ad-1546353474406-0" style="height:250px; width:300px;"><div id="google_ads_iframe_/1090369/C1_0__container__" style="border: 0pt none; width: 300px; height: 250px;"></div></div>
</div>
<div id="under_post_news"><div class="itnews c_ad_block"><b>相关博文：</b><br>·  <a href="https://www.cnblogs.com/jwdd/p/10156448.html" target="_blank" onclick="clickRecomItmem(undefined,&#39;undefined&#39;)">Springboot集成RabbitMQ</a><br>·  <a href="https://www.cnblogs.com/youcong/p/9387611.html" target="_blank" onclick="clickRecomItmem(undefined,&#39;undefined&#39;)">SpringBoot实战(八)之RabbitMQ</a><br>·  <a href="https://www.cnblogs.com/grasp/p/9448660.html" target="_blank" onclick="clickRecomItmem(undefined,&#39;undefined&#39;)">SpringBoot集成RabbitMQ</a><br>·  <a href="https://www.cnblogs.com/wadmwz/p/10320253.html" target="_blank" onclick="clickRecomItmem(undefined,&#39;undefined&#39;)">SpringBoot集成RabbitMQ</a><br>·  <a href="https://www.cnblogs.com/daizhj/archive/2010/10/25/1860442.html" target="_blank" onclick="clickRecomItmem(undefined,&#39;undefined&#39;)">NET下RabbitMQ实践[实战篇]</a><br></div></div>
<div id="cnblogs_c2" class="c_ad_block">
    <div id="div-gpt-ad-1539008685004-0" style="height:60px; width:468px;"><div id="google_ads_iframe_/1090369/C2_0__container__" style="border: 0pt none; width: 468px; height: 60px;"></div></div>
</div>
<div id="under_post_kb"><div class="itnews c_ad_block"><b>最新新闻</b>：<br> ·  <a href="https://news.cnblogs.com/n/621701/" target="_blank">中国联通混改元年业绩同比增8倍年内股价涨37.91%</a><br> ·  <a href="https://news.cnblogs.com/n/621700/" target="_blank">霍金去世一周年：一文看透他的光辉人生！</a><br> ·  <a href="https://news.cnblogs.com/n/621699/" target="_blank">外媒：美国正对脸书与科技巨头达成的数据协议进行刑事调查</a><br> ·  <a href="https://news.cnblogs.com/n/621698/" target="_blank">丁磊耐心不再，网易电商梦碎</a><br> ·  <a href="https://news.cnblogs.com/n/621697/" target="_blank">外媒：软银和其他投资者拟向Uber自动驾驶部门投资10亿美元</a><br>» <a href="http://news.cnblogs.com/" title="IT新闻" target="_blank">更多新闻...</a></div></div>
<div id="HistoryToday" class="c_ad_block"></div>
<script type="text/javascript">
 if(enablePostBottom()) {
    codeHighlight();
    fixPostBody();
    setTimeout(function () { incrementViewCount(cb_entryId); }, 50);
    deliverT2();
    deliverC1();
    deliverC2();    
    loadNewsAndKb();
    loadBlogSignature();
    LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid);
    GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate, cb_postType);
    loadOptUnderPost();
    GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate);  
}
</script>
</div>

    
	</div><!--end: forFlow -->
	</div><!--end: mainContent 主体内容容器-->

	<div id="sideBar">
		<div id="sideBarMain">
			
<!--done-->
<div class="newsItem">
<h3 class="catListTitle">公告</h3>
	<div id="blog-news"><!-- 爱心特效 -->
<script type="text/javascript">window['__document_write_ajax_callbacks__']['4']();</script><script type="text/javascript">window['__document_write_ajax_callbacks__']['1']();</script>

<!-- 分享 -->
<script type="text/javascript">window['__document_write_ajax_callbacks__']['5']();</script><script>window['__document_write_ajax_callbacks__']['2']();</script>

<!--  时间控件 -->
<div id="myTime">
        <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="160" height="70" id="honehoneclock" align="middle">
            <param name="allowScriptAccess" value="always">
            <param name="movie" value="http://chabudai.sakura.ne.jp/blogparts/honehoneclock/honehone_clock_wh.swf">
            <param name="quality" value="high">
            <param name="bgcolor" value="#ffffff">
            <param name="wmode" value="transparent">
            <embed wmode="transparent" src="http://chabudai.sakura.ne.jp/blogparts/honehoneclock/honehone_clock_wh.swf" quality="high" bgcolor="#ffffff" width="160" height="70" name="honehoneclock" align="middle" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
        </object>
</div><div id="profile_block">昵称：<a href="https://home.cnblogs.com/u/skychenjiajun/">名山丶深处</a><br>园龄：<a href="https://home.cnblogs.com/u/skychenjiajun/" title="入园时间：2017-12-29">1年2个月</a><br>粉丝：<a href="https://home.cnblogs.com/u/skychenjiajun/followers/">3</a><br>关注：<a href="https://home.cnblogs.com/u/skychenjiajun/followees/">1</a><div id="p_b_follow"><a href="javascript:void(0);" onclick="follow(&#39;ae035141-3266-4814-17e3-08d54dba4453&#39;)">+加关注</a></div><script type="text/javascript">window['__document_write_ajax_callbacks__']['6']();</script><script>window['__document_write_ajax_callbacks__']['3']();</script></div></div><script type="text/javascript">loadBlogNews();</script>
</div>

			<div id="blog-calendar" style=""><table id="blogCalendar" class="Cal" cellspacing="0" cellpadding="0" title="Calendar">
	<tbody><tr><td colspan="7"><table class="CalTitle" cellspacing="0">
		<tbody><tr><td class="CalNextPrev"><a href="javascript:void(0);" onclick="loadBlogCalendar(&#39;2019/02/01&#39;);return false;">&lt;</a></td><td align="center">2019年3月</td><td class="CalNextPrev" align="right"><a href="javascript:void(0);" onclick="loadBlogCalendar(&#39;2019/04/01&#39;);return false;">&gt;</a></td></tr>
	</tbody></table></td></tr><tr><th class="CalDayHeader" align="center" abbr="日" scope="col">日</th><th class="CalDayHeader" align="center" abbr="一" scope="col">一</th><th class="CalDayHeader" align="center" abbr="二" scope="col">二</th><th class="CalDayHeader" align="center" abbr="三" scope="col">三</th><th class="CalDayHeader" align="center" abbr="四" scope="col">四</th><th class="CalDayHeader" align="center" abbr="五" scope="col">五</th><th class="CalDayHeader" align="center" abbr="六" scope="col">六</th></tr><tr><td class="CalOtherMonthDay" align="center">24</td><td class="CalOtherMonthDay" align="center">25</td><td class="CalOtherMonthDay" align="center">26</td><td class="CalOtherMonthDay" align="center">27</td><td class="CalOtherMonthDay" align="center">28</td><td align="center">1</td><td class="CalWeekendDay" align="center">2</td></tr><tr><td class="CalWeekendDay" align="center">3</td><td align="center">4</td><td align="center">5</td><td align="center">6</td><td align="center">7</td><td align="center">8</td><td class="CalWeekendDay" align="center">9</td></tr><tr><td class="CalWeekendDay" align="center">10</td><td align="center">11</td><td align="center">12</td><td align="center">13</td><td class="CalTodayDay" align="center">14</td><td align="center">15</td><td class="CalWeekendDay" align="center">16</td></tr><tr><td class="CalWeekendDay" align="center">17</td><td align="center">18</td><td align="center">19</td><td align="center">20</td><td align="center">21</td><td align="center">22</td><td class="CalWeekendDay" align="center">23</td></tr><tr><td class="CalWeekendDay" align="center">24</td><td align="center">25</td><td align="center">26</td><td align="center">27</td><td align="center">28</td><td align="center">29</td><td class="CalWeekendDay" align="center">30</td></tr><tr><td class="CalWeekendDay" align="center">31</td><td class="CalOtherMonthDay" align="center">1</td><td class="CalOtherMonthDay" align="center">2</td><td class="CalOtherMonthDay" align="center">3</td><td class="CalOtherMonthDay" align="center">4</td><td class="CalOtherMonthDay" align="center">5</td><td class="CalOtherMonthDay" align="center">6</td></tr>
</tbody></table></div><script type="text/javascript">loadBlogDefaultCalendar();</script>
			
			<div id="leftcontentcontainer">
				<div id="blog-sidecolumn"><div id="sidebar_search" class="sidebar-block">
<div id="sidebar_search" class="mySearch">
<h3 class="catListTitle">搜索</h3>
<div id="sidebar_search_box">
<div id="widget_my_zzk" class="div_my_zzk"><input type="text" id="q" onkeydown="return zzk_go_enter(event);" class="input_my_zzk">&nbsp;<input onclick="zzk_go()" type="button" value="找找看" id="btnZzk" class="btn_my_zzk"></div>
<div id="widget_my_google" class="div_my_zzk"><input type="text" name="google_q" id="google_q" onkeydown="return google_go_enter(event)" class="input_my_zzk">&nbsp;<input onclick="google_go()" type="button" value="谷歌搜索" class="btn_my_zzk"></div>
</div>
</div>

</div><div id="sidebar_shortcut" class="sidebar-block">
<div class="catListLink">
<h3 class="catListTitle">常用链接</h3>
<ul>
<li><a href="https://www.cnblogs.com/skychenjiajun/p/" title="我的博客的随笔列表">我的随笔</a></li><li><a href="https://www.cnblogs.com/skychenjiajun/MyComments.html" title="我发表过的评论列表">我的评论</a></li><li><a href="https://www.cnblogs.com/skychenjiajun/OtherPosts.html" title="我评论过的随笔列表">我的参与</a></li><li><a href="https://www.cnblogs.com/skychenjiajun/RecentComments.html" title="我的博客的评论列表">最新评论</a></li><li><a href="https://www.cnblogs.com/skychenjiajun/tag/" title="我的博客的标签列表">我的标签</a></li>
</ul>
<div id="itemListLin_con" style="display:none;">
<ul>

</ul>
</div>
</div></div><div id="sidebar_toptags" class="sidebar-block">
<div class="catListTag">
<h3 class="catListTitle">我的标签</h3>
<ul>
<li><a href="https://www.cnblogs.com/skychenjiajun/tag/springboot/">springboot</a>(8)</li><li><a href="https://www.cnblogs.com/skychenjiajun/tag/springaop/">springaop</a>(2)</li><li><a href="https://www.cnblogs.com/skychenjiajun/tag/RabbitMQ/">RabbitMQ</a>(2)</li><li><a href="https://www.cnblogs.com/skychenjiajun/tag/redis/">redis</a>(2)</li><li><a href="https://www.cnblogs.com/skychenjiajun/tag/spring/">spring</a>(2)</li><li><a href="https://www.cnblogs.com/skychenjiajun/tag/CentOS/">CentOS</a>(2)</li><li><a href="https://www.cnblogs.com/skychenjiajun/tag/Linux/">Linux</a>(2)</li><li><a href="https://www.cnblogs.com/skychenjiajun/tag/maven/">maven</a>(1)</li><li><a href="https://www.cnblogs.com/skychenjiajun/tag/mq/">mq</a>(1)</li><li><a href="https://www.cnblogs.com/skychenjiajun/tag/Mysql/">Mysql</a>(1)</li><li><a href="https://www.cnblogs.com/skychenjiajun/tag/">更多</a></li>
</ul>
</div></div><div id="sidebar_categories">
<div id="sidebar_postcategory" class="catListPostCategory sidebar-block">
<h3 class="catListTitle">随笔分类</h3>

<ul>

<li><a id="CatList_LinkList_0_Link_0" href="https://www.cnblogs.com/skychenjiajun/category/1201353.html">Java(3)</a> </li>

<li><a id="CatList_LinkList_0_Link_1" href="https://www.cnblogs.com/skychenjiajun/category/1139378.html">Linux(5)</a> </li>

<li><a id="CatList_LinkList_0_Link_2" href="https://www.cnblogs.com/skychenjiajun/category/1205328.html">MQ(2)</a> </li>

<li><a id="CatList_LinkList_0_Link_3" href="https://www.cnblogs.com/skychenjiajun/category/1139401.html">NoSQL(1)</a> </li>

<li><a id="CatList_LinkList_0_Link_4" href="https://www.cnblogs.com/skychenjiajun/category/1200645.html">Spring(2)</a> </li>

<li><a id="CatList_LinkList_0_Link_5" href="https://www.cnblogs.com/skychenjiajun/category/1139375.html">Spring Boot(9)</a> </li>

<li><a id="CatList_LinkList_0_Link_6" href="https://www.cnblogs.com/skychenjiajun/category/1139377.html">Spring Cloud</a> </li>

<li><a id="CatList_LinkList_0_Link_7" href="https://www.cnblogs.com/skychenjiajun/category/1139383.html">SQL(2)</a> </li>

</ul>

</div>

<div id="sidebar_postarchive" class="catListPostArchive sidebar-block">
<h3 class="catListTitle">随笔档案</h3>

<ul>

<li><a id="CatList_LinkList_1_Link_0" href="https://www.cnblogs.com/skychenjiajun/archive/2018/05.html">2018年5月 (2)</a> </li>

<li><a id="CatList_LinkList_1_Link_1" href="https://www.cnblogs.com/skychenjiajun/archive/2018/04.html">2018年4月 (5)</a> </li>

<li><a id="CatList_LinkList_1_Link_2" href="https://www.cnblogs.com/skychenjiajun/archive/2018/03.html">2018年3月 (5)</a> </li>

<li><a id="CatList_LinkList_1_Link_3" href="https://www.cnblogs.com/skychenjiajun/archive/2018/01.html">2018年1月 (4)</a> </li>

<li><a id="CatList_LinkList_1_Link_4" href="https://www.cnblogs.com/skychenjiajun/archive/2017/12.html">2017年12月 (1)</a> </li>

</ul>

</div>

</div><div id="sidebar_recentcomments" class="sidebar-block"><div id="recent_comments_wrap">
<div class="catListComment">
<h3 class="catListTitle">最新评论</h3>

	<div id="RecentCommentsBlock"><ul>
        <li class="recent_comment_title"><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#4137644">1. Re:springboot集成rabbitmq（实战）</a></li>
        <li class="recent_comment_body">写的很详细！！！</li>
        <li class="recent_comment_author">--摩箜贼</li>
        <li class="recent_comment_title"><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html#4132852">2. Re:springboot集成rabbitmq（实战）</a></li>
        <li class="recent_comment_body">十分感谢您的分享，学习到了</li>
        <li class="recent_comment_author">--不锈钢奶豆</li>
        <li class="recent_comment_title"><a href="https://www.cnblogs.com/skychenjiajun/p/9057379.html#4103605">3. Re:springboot集成schedule（深度理解）</a></li>
        <li class="recent_comment_body">感谢博主的解答，原来是由于我的Task类不和主类在同一个包下，楼主给我发的demo里面就在同一个包下，若不在同一个包下，可以通过@ComponentScan(basePackages="com.fp.......</li>
        <li class="recent_comment_author">--Liam_fang</li>
        <li class="recent_comment_title"><a href="https://www.cnblogs.com/skychenjiajun/p/9057379.html#4103413">4. Re:springboot集成schedule（深度理解）</a></li>
        <li class="recent_comment_body">入口类@SpringBootApplication @EnableSchedulingpublic class HelloApplication {	private static final Logg......</li>
        <li class="recent_comment_author">--Liam_fang</li>
        <li class="recent_comment_title"><a href="https://www.cnblogs.com/skychenjiajun/p/9057379.html#4103411">5. Re:springboot集成schedule（深度理解）</a></li>
        <li class="recent_comment_body">@Liam_fang你加一下我微信skychenjiajun，我看看能否帮你解决...</li>
        <li class="recent_comment_author">--名山丶深处</li>
</ul>
</div>
</div>
</div></div><div id="sidebar_topviewedposts" class="sidebar-block"><div id="topview_posts_wrap">
<div class="catListView">
<h3 class="catListTitle">阅读排行榜</h3>
	<div id="TopViewPostsBlock"><ul><li><a href="https://www.cnblogs.com/skychenjiajun/p/8875456.html">1. Hello——Java10新特性，请了解一下(10143)</a></li><li><a href="https://www.cnblogs.com/skychenjiajun/p/9057379.html">2. springboot集成schedule（深度理解）(7316)</a></li><li><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html">3. springboot集成rabbitmq（实战）(5471)</a></li><li><a href="https://www.cnblogs.com/skychenjiajun/p/8269861.html">4. springboot集成mybatis（一）(5405)</a></li><li><a href="https://www.cnblogs.com/skychenjiajun/p/8608310.html">5. springboot集成redis（mybatis、分布式session）(2213)</a></li></ul></div>
</div>
</div></div><div id="sidebar_topcommentedposts" class="sidebar-block"><div id="topfeedback_posts_wrap">
<div class="catListFeedback">
<h3 class="catListTitle">评论排行榜</h3>
	<div id="TopFeedbackPostsBlock"><ul><li><a href="https://www.cnblogs.com/skychenjiajun/p/9057379.html">1. springboot集成schedule（深度理解）(7)</a></li><li><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html">2. springboot集成rabbitmq（实战）(4)</a></li><li><a href="https://www.cnblogs.com/skychenjiajun/p/8875456.html">3. Hello——Java10新特性，请了解一下(1)</a></li></ul></div>
</div>
</div></div><div id="sidebar_topdiggedposts" class="sidebar-block"><div id="topdigg_posts_wrap">
<div class="catListView">
<h3 class="catListTitle">推荐排行榜</h3>
<div id="TopDiggPostsBlock"><ul><li><a href="https://www.cnblogs.com/skychenjiajun/p/9037324.html">1. springboot集成rabbitmq（实战）(3)</a></li><li><a href="https://www.cnblogs.com/skychenjiajun/p/8857713.html">2. springaop——AspectJ不可不知的细节(1)</a></li><li><a href="https://www.cnblogs.com/skychenjiajun/p/9057379.html">3. springboot集成schedule（深度理解）(1)</a></li></ul></div>
</div></div></div></div><script type="text/javascript">loadBlogSideColumn();</script>
			</div>
			
		</div><!--end: sideBarMain -->
	</div><!--end: sideBar 侧边栏容器 -->
	<div class="clear"></div>
	</div><!--end: main -->
	<div class="clear"></div>
	<div id="footer">
		
<!--done-->
Copyright ©2019 名山丶深处
	</div><!--end: footer -->
</div><!--end: home 自定义的最大容器 -->



<audio controls="controls" style="display: none;"></audio><script type="text/javascript">
(function() {
    var $backToTopTxt = "返回顶部", $backToTopEle = $('<div class="backToTop"></div>').appendTo($("body"))
        .text($backToTopTxt).attr("title", $backToTopTxt).click(function() {
            $("html, body").animate({ scrollTop: 0 }, 120);
    }), $backToTopFun = function() {
        var st = $(document).scrollTop(), winh = $(window).height();
        (st > 0)? $backToTopEle.show(): $backToTopEle.hide();
        //IE6下的定位
        if (!window.XMLHttpRequest) {
            $backToTopEle.css("top", st + winh - 166);
        }
    };
    $(window).bind("scroll", $backToTopFun);
    $(function() { $backToTopFun(); });
})();
</script><div class="backToTop" title="返回顶部" style="display: block;">返回顶部</div><script type="text/javascript">
(function(window,document,undefined){
        var hearts = [];
        window.requestAnimationFrame = (function(){
                return window.requestAnimationFrame || 
                           window.webkitRequestAnimationFrame ||
                           window.mozRequestAnimationFrame ||
                           window.oRequestAnimationFrame ||
                           window.msRequestAnimationFrame ||
                           function (callback){
                                   setTimeout(callback,1000/60);
                           }
        })();
        init();
        function init(){
                css(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
                attachEvent();
                gameloop();
        }
        function gameloop(){
                for(var i=0;i<hearts.length;i++){
                    if(hearts[i].alpha <=0){
                            document.body.removeChild(hearts[i].el);
                            hearts.splice(i,1);
                            continue;
                    }
                    hearts[i].y--;
                    hearts[i].scale += 0.004;
                    hearts[i].alpha -= 0.013;
                    hearts[i].el.style.cssText = "left:"+hearts[i].x+"px;top:"+hearts[i].y+"px;opacity:"+hearts[i].alpha+";transform:scale("+hearts[i].scale+","+hearts[i].scale+") rotate(45deg);background:"+hearts[i].color;
            }
            requestAnimationFrame(gameloop);
        }
        function attachEvent(){
                var old = typeof window.onclick==="function" && window.onclick;
                window.onclick = function(event){
                        old && old();
                        createHeart(event);
                }
        }
        function createHeart(event){
            var d = document.createElement("div");
            d.className = "heart";
            hearts.push({
                    el : d,
                    x : event.clientX - 5,
                    y : event.clientY - 5,
                    scale : 1,
                    alpha : 1,
                    color : randomColor()
            });
            document.body.appendChild(d);
    }
    function css(css){
            var style = document.createElement("style");
                style.type="text/css";
                try{
                    style.appendChild(document.createTextNode(css));
                }catch(ex){
                    style.styleSheet.cssText = css;
                }
                document.getElementsByTagName('head')[0].appendChild(style);
    }
        function randomColor(){
                return "rgb("+(~~(Math.random()*255))+","+(~~(Math.random()*255))+","+(~~(Math.random()*255))+")";
        }
})(window,document);
</script><script type="text/javascript">window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"slide":{"type":"slide","bdImg":"6","bdPos":"right","bdTop":"100"},"image":{"viewList":["qzone","tsina","tqq","renren","weixin"],"viewText":"分享到：","viewSize":"16"}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script><script type="text/javascript">getFollowStatus('ae035141-3266-4814-17e3-08d54dba4453')</script></body><style type="text/css">#yddContainer{display:block;font-family:Microsoft YaHei;position:relative;width:100%;height:100%;top:-4px;left:-4px;font-size:12px;border:1px solid}#yddTop{display:block;height:22px}#yddTopBorderlr{display:block;position:static;height:17px;padding:2px 28px;line-height:17px;font-size:12px;color:#5079bb;font-weight:bold;border-style:none solid;border-width:1px}#yddTopBorderlr .ydd-sp{position:absolute;top:2px;height:0;overflow:hidden}.ydd-icon{left:5px;width:17px;padding:0px 0px 0px 0px;padding-top:17px;background-position:-16px -44px}.ydd-close{right:5px;width:16px;padding-top:16px;background-position:left -44px}#yddKeyTitle{float:left;text-decoration:none}#yddMiddle{display:block;margin-bottom:10px}.ydd-tabs{display:block;margin:5px 0;padding:0 5px;height:18px;border-bottom:1px solid}.ydd-tab{display:block;float:left;height:18px;margin:0 5px -1px 0;padding:0 4px;line-height:18px;border:1px solid;border-bottom:none}.ydd-trans-container{display:block;line-height:160%}.ydd-trans-container a{text-decoration:none;}#yddBottom{position:absolute;bottom:0;left:0;width:100%;height:22px;line-height:22px;overflow:hidden;background-position:left -22px}.ydd-padding010{padding:0 10px}#yddWrapper{color:#252525;z-index:10001;background:url(chrome-extension://eopjamdnofihpioajgfdikhhbobonhbb/ab20.png);}#yddContainer{background:#fff;border-color:#4b7598}#yddTopBorderlr{border-color:#f0f8fc}#yddWrapper .ydd-sp{background-image:url(chrome-extension://eopjamdnofihpioajgfdikhhbobonhbb/ydd-sprite.png)}#yddWrapper a,#yddWrapper a:hover,#yddWrapper a:visited{color:#50799b}#yddWrapper .ydd-tabs{color:#959595}.ydd-tabs,.ydd-tab{background:#fff;border-color:#d5e7f3}#yddBottom{color:#363636}#yddWrapper{min-width:250px;max-width:400px;}</style></html>